CodeIgniter Forums
Dynamic routing... - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forumdisplay.php?fid=23)
+--- Thread: Dynamic routing... (/showthread.php?tid=38014)



Dynamic routing... - El Forum - 01-27-2011

[eluser]William Rufino[/eluser]
Hello everyone,

I have a question for you "codeigniter pros".

i want to create a route $route['(:any)'] = 'users/get/$1';

www.mysite.com/williamhrs

BUT if I do that, i'll have to create every route possible have manually, and that wouldn't be good cuz there is a lot of them!

is there a way redirect to another route if $1 isn't found?

Sorry for my english, it is a little rusty!


Dynamic routing... - El Forum - 01-27-2011

[eluser]toopay[/eluser]
Of course, it would be terrible. if you expecting to have a uri such 'www.mysite.com/williamhrs', apparently you have to define all the names of your controller, even its function, into a verryy long line routes configuration...

maybe, the best way is to adding some little prefix in your route, like

Code:
$route['u_([a-z]+)'] = "users/get/$1";



Dynamic routing... - El Forum - 01-27-2011

[eluser]William Rufino[/eluser]
Would it be possible to make a hook to check is the user exists, if it a user it goes to the user controller, if not it keeps going where'ever it was or 404 ?


Dynamic routing... - El Forum - 01-27-2011

[eluser]toopay[/eluser]
I think, its possible(if you working around it for a while). There are many ways to get the url, as above. You can write configuration route, or intercept (hacking, lol) core library. But, internally, the concept of CodeIgniter url is described on http://ellislab.com/codeigniter/user-guide/general/urls.html


Dynamic routing... - El Forum - 01-27-2011

[eluser]DBPolito[/eluser]
You can do it of many ways. Here the easier way:

$route['^((?!controller|folders|you_dont_wanna_redirect)\S*)'] = "user/get/$1";

so, in user controller you check in DB if user exists, if not, show 404 page or redirect it.

That's fix?