I have a Real Estate Site, in CodeIgniter my link names is:
htp://examplesite(dot)com/property/31/en/property_title
I want to replace it with:
htp://examplesite(dot)com/my-custom-slug.htm
or
htp://examplesite(dot)com/property_title
Both ways might be ok. My routes are:
$route['default_controller'] = "frontend";
$route['404_override'] = 'frontend';
/* Custom routing */
$route['admin'] = 'admin/dashboard';
//$route['secret'] = 'admin/dashboard';
// To change property->listing in uri
$route['listing/(:num)/(:any)/(:any)'] = "property/$1/$2/$3";
$route['listing/(:num)/(:any)'] = "property/$1/$2";
what is the best way in this case?
I know there is a possible solution that:
Add
$config['listing_uri'] = 'listing';
Than add to:
In ‘application\config\routes.php’
After:
$route['listing/(:num)/(:any)/(:any)'] = "property/$1/$2/$3";
$route['listing/(:num)/(:any)'] = "property/$1/$2";
Add also:
$route['changed/(:num)/(:any)/(:any)'] = "property/$1/$2/$3";
$route['changed/(:num)/(:any)'] = "property/$1/$2";
but is there any way to remove the 'listing', to see only the clean url (for better seo purpose) ??
Welcome, luckily your problem isn't too big :)
Maybe have one slug before the property title to make the solution a bit more scalable:
$route["listing/(:any)"] = "property/view/$1";
Then point your browser to yoursite.com/listing/some_slug or yoursite.com/listing/12
You will need to have a slug associated with each property, otherwise you can use the id, and in the view function just retrieve the property based on the slug or id parameter.
If you don't want to include the listing/ you might have problems down the line when trying to create other routes.
Related
I use codeigniter v.3 and I have the following url in most of my pages:
www.domain.buu/page/index/105
Where Page is Controller Name, index is the name of the function and the 105 is the id of the record.
What I would like to have is www.domain.buu/my_custom_page_name
Where my_custom_page_name can be the something like within the routes.php
105 = "my_custom_page_name"
106 = "my_other_custom_page_name"
107 = "some_other_page_name"
Is this possible to be done? CodeIgniter Manual is not very helpful on this. I tried to change the routes with the following code:
$route['index/page/:num'] = 'my_custom_page_name';
but it didn't work.
You were on the right tracks with your route attempt, but you need to flip the arguments around.
The array key in $route is the URI the user will see, such as my_custom_page_name in your case.
The value is the controller and method you want to use, such as page/index/105
In your case, the route may be like this:
$route['my_custom_page_name'] = 'index/page/105';
If you are trying to implement seo-friendly URL, you can use slug instead of 105(assuming its a unique id) by adding an additional column in your database named slug
For instance, http://www.example.com/page/this-is-my-slug/
while using routes.php , you need to duplicate the route setting for all entries.
$route['my_custom_page_name'] = 'index/page/105';
$route['my_custom_page_name-new'] = 'index/page/106';
I'm not sure what you trying to achive but maybe this:
$route['my_custom_page_name/(:num)'] = 'page/index/$1';
So, I'm struggeling with this problem.
I got these two types of URLS:
localhost/en/women
localhost/en/women/products
And I'm trying to tell CodeIgniter to use route X if its localhost/en/women but use route Y if its localhost/en/women/products, but It always uses the X route instead.
I tried to exclude the /products segment by using regex like this:
$route['^en/women(?!products)(.*)'] = "women";
$route['^en/women/products'] = "products";
But I can't seem to get it to work.
I hope you get what I'm looking for here.
Any feedback is appriciated.
$route['en/women/?'] = "women";
$route['en/women/products'] = "products";
Should solve this?
Basicly ^ and $ exist in the regexp by default.
Or maybe you need a:
$route['^en/women/products'] = "products";
$route['^en/women/?'] = "women";
Just have the products above, and it should override.
Use CI's WildCards!
$route['en/women'] = "women";
$route['en/women/(:any)'] = "products";
I have searched for the solution to my problem in CI user guide and on Stackoverflow as well but couldn't find. So, here is my problem.
I need to build SEO friendly URLs. I have a controller called "Outlets" and the view that I need to generate will have a URL structure like http://www.mysite.com/[city]/[area]/[outlet-name].
The segments city and area are fields in their respective tables and outlet_name is a field in the table "Outlets".
I am able to generate a URL like http://www.mysite.com/outlets/123 but how do I add the city and area name to the URL.
If all of your page use the same controller, in config/routes.php add this...
$route['(:any)/(:any)/(:any)'] = "outlets/$1/$2/$3";
$route['(:any)/(:any)/(:any)/(:any)'] = "outlets/$1/$2/$3/$4"; // fixed typo
In the controller you will want to remap the function calls because Codeigniter will be looking for functions with the names of the city and they will not exist.
http://codeigniter.com/user_guide/general/controllers.html#remapping
public function _remap($city, $area, $outlet, $options = '')
{
$this->some_function_below($city, $area, $outlet, $options);
}
Another alternative solution.
You can use URI segments. For an url like http://www.mysite.com/[city]/[area]/[outlet-name]
<?php
$this->uri->segment(3); // city
$this->uri->segment(4); // area
$this->uri->segment(5); // outlet-name
?>
and so on... See http://codeigniter.com/user_guide/libraries/uri.html for more details.
I was wondering how i would go about implementing my site's URLs like http://mysite.com/ArticleTitleHere using zend framework.
The way i see it is to implement a regex route, but this way zf will not be able to tell say an article URL from a regular module/controller/action or controller/action routes. How would i go about implementing such a thing?
Thanks
you can use placeholders for that in your route:
routes.test.route = "/:article"
routes.test.defaults.module = default
routes.test.defaults.controller = test
routes.test.defaults.action = test
routes.test.defaults.article = ""
You can retrieve the article parameter in your controller using $this->_getParam("article"). The rest (matching the database entry by the article's name) is your part.
But you should add something like an url-prefix and/or article id, otherwise other routes may not be found. So use something like this is much better:
routes.test.route = "/article/:id/:title"
routes.test.defaults.module = default
routes.test.defaults.controller = test
routes.test.defaults.action = test
routes.test.defaults.article = ""
routes.test.defaults.id = ""
You can use the url-Viewhelper this way to create a link:
<?php echo $this->escape($article->title); ?>
And in your controller for a redirect:
$this->_helper->redirector->gotoRoute(array("id" => $article->id, "title" => $article->title), "test");
Keep in mind escaping or filtering the $article->title for invalid characters (or non-ascii-characters).
I am developing an ecommerce website with CI that has product categories and products. I want to route the URL so that it will go to the products controller, then run the getCategoryByName function for the first segment, then run the getProductByName for the second segment. Here is what I have:
URL:
products/docupen/rc805
routes.php:
$route['products/([a-z]+)'] = "products/getCategoryByName/$1";
$route['products/([a-z]+)/([a-z0-9]+)'] = "products/$1/getProductByName/$2";
But its not working. "docupen" is the category, and "rc805" is the product.
Thanks in advance.
Thank you all for your help. This is what I ended up with for what I needed.
$route['products/:any/:num'] = "products/getProductByID";
$route['products/:any/:any'] = "products/getProductByName";
$route['products/:any'] = "products/getCategoryByName";
My answer builds a bit on Colin's answer.
When I played around with the routes in CodeIgniter I came to the conclusion that the order of the routes was important. When it finds the first valid route it won't do the other routes in the list. If it doesn't find any valid routes then it will handle the default route.
My routes that I played around with for my particular project worked as follows:
$route['default_controller'] = "main";
$route['main/:any'] = "main";
$route['events/:any'] = "main/events";
$route['search/:any'] = "main/search";
$route['events'] = "main/events";
$route['search'] = "main/search";
$route[':any'] = "main";
If I entered "http://localhost/index.php/search/1234/4321" It would be routed to main/search and I can then use $this->uri->segment(2); to retrieve the 1234.
In your scenario I would try (order is very important):
$route['products/:any/:any'] = "products/getProductByName";
$route['products/:any'] = "products/getCategoryByName";
I don't know enough to route the way you wanted (products/$1/getProductByName/$2), but I'm not sure how you would create a controller to handle this particular form of URI. Using the $this->uri->segment(n); statements as mentioned by Colin in your controller, you should be able to do what you want.
You should use the URI class to retrieve the "docupen" and "rc805" segments from your url. You can then use those values in whatever functions you need.
For example, if your url is www.yoursite.com/products/docupen/rc805, you would use the following in your products controller to retrieve the segments and assign them to variables:
$category = $this->uri->segment(2); //docupen
$product = $this->uri->segment(3); //rc805
Then you can use $category and $product however you need to.
CodeIgniter routes don't work well with regex. They are supported, not I can never get them to work. It would be much easier to catch them like this
$route['products/(:any)'] = "products/getCategoryByName/$1";
$route['products/(:any)/(:any)'] = "products/$1/getProductByName/$2";