Welcome Guest, Not a member yet? Register   Sign In
Help undestanding Routes?
#1

[eluser]gRoberts[/eluser]
hi all,

I am having some problems trying to understand the routes situation.

I know how I can resolve the issue, but I don't want to go changing CI code if it can be done with Routes.

Basically, what I want to do is every time a controller is requested, I want to check if that controller name is actually a username within my database.

If it is, then I want to route it to /user/view and either pass the username or I can use uri->segment to get it.

If it isn't, then I want it to continue what it normally does, either show the controller requested or throw an error if it doesn't exist.

Does anyone have any idea's?

Thanks

Gav
#2

[eluser]Michael Wales[/eluser]
You have to approach it in the opposite way - you define all of your known controllers, then you define an open rule for all of your users.

Check out the following code, which accomplishes something similar (although I am using it for a CMS type system). I've added some comments to better explain:

Code:
// Match /admin/pages/delete/about-me to the pages controller, delete method, about-me parameter
$route['admin/([a-z]+)/([a-z]+)/([a-zA-Z0-9_-]+)'] = "$1/$2/$3";
// Match /admin/pages/delete to the pages controller, delete method
$route['admin/([a-z]+)/([a-z]+)'] = "$1/$2";
// Match /admin/pages to the admin controller, pages method
// Within the admin controller, that method simply redirects to the appropriate administrative method within the pages controller
$route['admin/([a-z]+)'] = "admin/$1";
// Match /admin/ to the admin controller
$route['admin'] = "admin";
// Match a request for root to the pages controller (would use the index method, which display a page passed as a parameter.
// Since no parameter is defined, it default to a page called home
$route['default_controller'] = "pages";
// Matches any other request to the pages controller, index method, with the first segment as a parameter
// Example: /about-me to /pages/index/about-me
// The index method of the pages controller uses that segment to retrieve database data and sends it to a view
$route['([a-zA-Z0-9_-]+)'] = "pages/index/$1";

This code could be optimized some I am sure - I'm not the biggest whiz on RegEx
#3

[eluser]gRoberts[/eluser]
I thought that was the case.

I was hoping it wasn't though as its now restricting CI from what it excels in. If my client doesn't like www.domain.com/user/view/username then i'll have to edit the code or go the way you suggested.

Thanks
#4

[eluser]Sarfaraz Momin[/eluser]
You can also check the _remap function in the controller to acheive something similar to what you require. I have a site where www.domain.com/username sortta URLs are working gr8.




Theme © iAndrew 2016 - Forum software by © MyBB