[SOLVED!] Friendly URL and removing controller name? |
[eluser]fancms[/eluser]
I'm not even sure this is possible, but here goes: I'd like to turn this: http://site.com/page/category-title/page...title.html into this: http://site.com/category-title/page-id/page-title.html Essentially, removing the controller (page) from the url. I have been playing with the routing features but can't quite figure out how to do this for just the pages controller. Any other routes - currently I have news/view/blah, among a couple of others - get directed to the page controller if I try doing a total wildcard like (.*)/(.*) (which I expected). Any help would be appreciated; I tried looking at the documentation for the _remap function but there really isn't much for it, and I'm not sure that's the way I need to go anyway.
[eluser]thurting[/eluser]
It probably isn't a good idea to structure things like that, but it can be done. The trick is to write a * rule and then overwrite it for the other controllers. However, if you have something like the following, site.com/news (news controller) site.com/category/id/title (page controller) you have to be careful that you never have a page "category" that overlaps with any of your controller names. It is really a recipe for disaster. Can I ask why you want to remove the controller name anyway? Users don't really care about URLs - most don't even look at them.
[eluser]Michael Wales[/eluser]
Code: $route[':any/:num/:any'] = "page/view/$2"; That route would translate the url (domain.com/site-news/36/site-launches-successfully.html) to the Page->view($id) method. I would probably rewrite it to use RegEx, but I always have to go look that stuff up.
[eluser]David Rothera[/eluser]
Would using that route means that if you had blah/blah/2 it wouldnt go to page/view?
[eluser]fancms[/eluser]
Quote:you have to be careful that you never have a page “category” that overlaps with any of your controller names.That would be easy to do with a simple callback check when they are adding page categories Quote:Can I ask why you want to remove the controller name anyway? Users don’t really care about URLs - most don’t even look at them.I'm targeting a specific niche with the program I'm writing and from all the research I did and from asking the webmasters what they liked, this is what they'd want to have for their content urls. The other controllers are fine as-is (ie, news/view/3/blah and links/view/3/blah). Quote: Thanks, Michael! Using your example and a bit of experimenting I came up with this: Code: $route['([a-z0-9-_]+)/([0-9]+)/([a-z0-9-_]+)'] = 'page/view/$2'; This allows alphanumeric characters with dashes (-) and underscores (_). The other controllers still work as they should. Thanks again! :lol:
[eluser]Michael Wales[/eluser]
The only thing you need to watch out for are other potential URLs that could meet the requirement for this rule. If you come across those - simply define a stricter rule prior to this catch-all rule. The following example makes little sense (SEO-wise), but proves the point: domain.com/admin/4/edit would qualify for the catch-all rule, pointing it to Page->view(4) - obviously not what we want. We want it to go to Admin->edit_users() - here's how to correct: Code: $route['admin/:num/:any'] = 'admin/edit_users'; Like I said, the example itself makes little sense. The most common place I use these catch-all style routing is when I want to use a domain.com/username style of URL for member profiles. I define all of my known routes, then a catch-all route for anything else. Code: $route['signup'] = 'users/signup';
[eluser]Edemilson Lima[/eluser]
I am having a problem with a routing... At my routes.php, I placed: Code: $route['([a-z]+)/([0-9]+)'] = "$1/details/index/$2"; If I try to access something like: http://www.site.com/products/details/index/3 It works fine. But, if I do it by the route: http://www.site.com/products/3 It calls products/details/index, but does not send the id code: Code: A PHP Error was encountered "products" is a folder, "details" is the controller and "index" is its default function. Any ideas? |
Welcome Guest, Not a member yet? Register Sign In |