URL Routing: With or without trailing slash |
[eluser]jonnyjon[/eluser]
Hi, I have a route set up: $route['directory/(:any)'] = "controller/function/$1"; The problem is that... This works: http://example.com/directory/whatever But this doesn't work: http://example.com/directory I know I could add a second route: $route['directory'] = "controller/function"; But is there a way to incorporate a reg ex to handle both cases in one statement? This this (but this doesn't work): $route['directory[\/*](:any)'] = "controller/function/$1";
[eluser]jonnyjon[/eluser]
Thanks! $route['directory(\/*|)([A-Za-z0-9]*)'] = "controller/function/$2"; Works great!
[eluser]Colin Williams[/eluser]
Code: $route[’directory(.*)’] = “controller/function$1”;
[eluser]Bramme[/eluser]
Colin's is the shortest, I use it too... Works like a breeze.
[eluser]jonnyjon[/eluser]
Yes, Colin's method is good but would match any number of sub-directories. ex: http://example.com/directory/subdir/subdir/subdir/ I just want to match: http://example.com/directory/subdir/ OR http://example.com/directory This is my updated reg expression: $route['directory\/?([a-zA-Z0-9_]*)'] = "controller/function/$1";
[eluser]Colin Williams[/eluser]
True, jonnyjon. The method I lined out is what I do to effectively 100% alias a Controller, which I think is extremely useful. When naming a Controller, it's good to name it in the context of the role it serves in your application. However, the URL should be named with consideration to a number of factors, like a users mental model, applications brand, etc. The above method allows for that with 0 headache... not that you needed or asked for that advice, but there it is ![]() |
Welcome Guest, Not a member yet? Register Sign In |