How to make my codigniter url seo friendly.
this is my url
http://localhost/picker2/web/search?category=doctor
i want url like this
http://localhost/picker2/web/search/doctor/
read this article www.askaboutphp.com/58/codeigniter-mixing-segment-based-url-with-querystrings.html
to create url like this : http://heloo.com/article/codeigniter-based-url
add this code in routes.php
$route['article/(:any)'] = "article/readmore/$1";
description :
article : class name
readmore : method from class article
$1 : get value from uri segment 2 value
Actually you have two options
1) Using Routes (this one already denyptw discussed right ?)
2) use the URL Helper url_title function
Note : You can use Parameter instead of query string
http://localhost/picker2/web/search?category=doctor
http://localhost/picker2/web/search/doctor/
web controller , search function , doctor parameter
example :
class Web extends CI_Controller
{
public function search($value)
{
//use this $value in your searching logic
}
}
You need Example of URL Helper
check this link Codeigniter URL: How to display id and article title in the URL
you can do this using routes.(Elislab)
as a simple trick.(Using Controllers)
each and every page create a controller. (ex: if contact create contact controller, if product create product controller.) So each and every link will be look like(if you click product) www.example.com/product,(if you click contact) www.example.com/contact.
If you want something to do with your product then write that method inside the product controller.(ex if you create cart then URL show www.example.com/product/cart )
Related
I don't want to show /route_name/{id} in the URL field of my Laravel project. Instead of that I want to show /route_name/{name} but pass the id in the back-end to the controller.
Suppose I have a route named departments and pass an id 3 named knee_pain as a parameter. And it is like /departments/3
But I want to to show /departments/knee_pain in my url and as well as want to pass the id 3 in my controller without showing the id in the url.
How to do that ?
In your model you can use the getRouteKeyName method to bind to another attribute than the default id in your routes :
public function getRouteKeyName()
{
return 'slug'; // Default is 'id'.
}
Rather than using the name attribute, that you could use elsewhere in your application for displaying the name of the entry, I recommend using an attribute made url friendly. You could use Str::slug() for that.
public function setNameAttribute($value) {
$this->name = $value;
$this->slug = \Str::slug($value);
}
It will 'slugify' your string, for example : \Str::slug('Knee pain') => 'knee-pain'.
Note : in Laravel 5.5, use the str_slug() helper.
You should also make sure this string is unique in your database.
First you have to garantee that the name is unique, if don't you will have more than one Id in your controller. For that i recommend you to use Purifier to remove spaces and make it URL friendly:
Purifier
Second, probably the best way to have clean controllers is creating a middleware that understand what kind of name is (what table should middleware look for). You can validate that by route name and send the correct id to controller.
Middleware docs
URL: www.blog.com/posts/category/post-title
How do I create a controller, that accepts the two parts of the URL "category" and "post-title" and echoes out the post?
Thank you a lot.
Timo
In CodeIgniter the URL is mapped as domain/Controller/Method/params, so following URL
www.blog.com/posts/category/post-title
will be mapped as posts is the controller, category as method name and rest are parameters. So, You need to create a controller like this:
// posts.php
class Posts extends CI_Controller {
public function show($category, $title)
{
// ...
}
}
Then the URL could be www.blog.com/posts/show/some-category/post-title and you can retrieve the some-category and post-title as parameters in you show method. For more information, check Controllers on CI User Guide.
I am in the process of creating a new website which loads all master and child categories from the database. I have tested the navigation as well, i.e., if I click any master category, it perfectly loads all the respective child categories without any issue. However, at present, I am doing this by passing query string in the URL. For instance
http://localhost/MyController?id=32145
Let's assume that the id, 32145, represents a master category namely 'About us'. My question is how can I change the above URL to something like:
http://localhost/Aboutus
and if there is any child category under About us than it should display as:
http://localhost/Aboutus/Mission
Please help me out as I am really stuck with this problem.
by default CodeIgniter uses a segment-based approach, you can do URL routing in way like your second part of the question - "and if there is any child category under About us"
$route['product/(:any)'] = "catalog/product_lookup";
more here: https://ellislab.com/codeigniter/user-guide/general/routing.html
but if you want to rewrite complete URL than you should probably check .htaccess rewriting
It is not easy, do once for migration.
In database you can store the New controller/url for products (if it is not have yet)
Create new Controllers
Route controller which redirect the old Url to the New Url
controllers
Route old urls to Route controller
Route controller something like this:
public function old_url($aProdId) {
if (is_null($aProdId)) {
// error cannot be null
}
$NewUrl = $this->new_url_model->getNewUrl($aProdId);
if (!$NewUrl) {
// error new url not exist
return;
}
redirect(base_url($NewUrl), 'refresh');
}
My url is: localhost/school/DPC
In the above url school is my controller class and DPC is the school name. I want to get the name DPC from the url in the school controller class.
In routes.php page I have used
$route['default_controller'] = "school";
Now what do I have to do such that I can get the school name from the url.
I think you want to create a profile page like other website are using ... abc.com/hameed
right?
If yes then in Route you should also add this
$route['school/(:any)'] = "school/profile/$1";
And in your school controller you can do this
function profile($school_name){
echo $school_name ;
}
According to your question, " localhost/school/DPC " , it will be intepreted by codeigniter in the following way:
'school' as controller and 'DPC' as a function name of 'school' controller class.
Since you are expecting to send DPC directly through url, you should make one function inside school class say 'myFunction' with a parameter say '$name' then your url will look like "localhost/school/myFunction/DPC" then only you can get your desired result.
Remember: the url format of CI is "hostname/controller/function/parameter". You are missing the function name that accepts that parameter.
I have a custom section that uses a custom layout. This section is dynamic and all the actions will receieve a YEAR parameter. I don't want setup navigation via section from inside the views. What would be the best way to get this parameter in a navigation action utilized by the layout? If the year is in the URL as ?year=2012 then I can get it via query string. My problem is that this may be in there as controller/action/2012. How can I get the year now in navigation action?
controller/action?year=2012 and controller/action/2012 are basically same if you have defined your route as
{controller}/{action}/{year}
In the action
public class MyController:Controller
{
public ActionResult MyAction(int year)
{
:
:
:
}
}
If you have defined above route along with the default route ( {controller}/{action}/{id} )
Both the url MyController/MyAction?year=2012 and MyController/MyAction/2012 will be treated equally.