Welcome Guest, Not a member yet? Register   Sign In
Routing before 404
#1

[eluser]Ben Knowles[/eluser]
In my application I allow users to have their own URI such as /~username. The controller I have set up for the user page is /users/index/username so they can use this or /~username. I currently have /~(.+) routed to this controller but I would prefer not to use the ~ character.

What I want to do is set up a route so any URI that hasn't been matched to a controller is routed to the user controller.

The only solution I've found so far is to extend the CI_Router class. I extended this class and modified the _validate_request function to load a controller instead of the show_404 function. This seems to work well enough, but is there a better way of doing this without having to extend the router class?

Thanks
#2

[eluser]marcoss[/eluser]
You can use the _remap() function inside your controller.
#3

[eluser]Ben Knowles[/eluser]
How would that work? I don't think it will know which controller to use. It needs to load a specific controller when a controller isn't found.

e.g. Say I have these pages and a controller for each one:
/home
/register
/login

Then I register and make my username 'ben'. I could then go to /ben (or anything not matching the controllers above) and it will load the user controller. I can't see how this can be done using remap.
#4

[eluser]Michael Wales[/eluser]
What I usually do - and this depends on how many "predefined controllers" you have, is just use routes.php. Like I said, if you have a lot of "non-user" controllers, it can get a bit hectic.

Code:
$route['about'] = 'info/about';
$route['faq'] = 'info/faq';
$route['login'] = 'user/login';
$route['logout'] = 'user/logout';
$route['(a-zA-Z0-9)+'] = 'user/profile/$1'; // Anything that didn't match the above routes
#5

[eluser]Ben Knowles[/eluser]
Sounds like a good solution. If I don't end up with too many controllers then I'll just add them all to routes.php

Thanks for the help




Theme © iAndrew 2016 - Forum software by © MyBB