CodeIgniter Forums
Route question - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20)
+--- Forum: Archived General Discussion (https://forum.codeigniter.com/forumdisplay.php?fid=21)
+--- Thread: Route question (/showthread.php?tid=17466)

Pages: 1 2


Route question - El Forum - 04-05-2009

[eluser]JulianM[/eluser]
I have a question about routes.

I would like to configure a route rule to get a behavior similar to many online applications when dealing with usernames. For example, my goal is something like this:

www.domain.com/username

But, taking in mind that I may have other controllers or pages, for example: /admin , /settings, /user, etc.

So, I would like to support my current controllers but in case there is no such controller and the user points to www.domain.com/username, I want to take that URL part as username and redirect to my a specific controller, ie www.domain.com/user/<username>

I know I need to distinguish about usernames and ordinal controllers. Maybe you have an answer for achieve this behavior and what are the best practices to do something like that?


Route question - El Forum - 04-06-2009

[eluser]tomcode[/eluser]
You can do
Code:
// first the static controllers
$route['static1'] = "static1";
$route['static2'] = "static2";
$route['static3'] = "static3";

// anything else goes to welcome
$route[':any'] = "welcome";



Route question - El Forum - 04-06-2009

[eluser]xwero[/eluser]
The problem with one segment urls where you don't know the value of can mess up the url structure of the site. For example if you have a admin section that can be reached with the first segment admin and a user chooses the name admin, how will you know which url is wanted?

A simple solution is to use a begin character that no controller or action uses for example a hypen so -admin goes to your user controller and admin goes to the admin controller and this with only one route
Code:
$route['-.+'] = 'user';

If you want to use user/<username> you have to route twice because the second segment identifies the method so you have to do user/method/<username> and the route will be
Code:
$route['-(.+)'] = 'user/method/$1';



Route question - El Forum - 04-06-2009

[eluser]tomcode[/eluser]
Well he can check on creation of the username :

Code:
$not_allowed = array('admin', 'static1', 'static2', 'static2');

if(in_array($username, $not_allowed)) return 'Not allowed';



Route question - El Forum - 04-06-2009

[eluser]xwero[/eluser]
Why should users not be allowed to a legal username, there will be other restrictions i assume, [a-zA-Z0-9\.\-_] for example. And what if the first segments change?

Routes are great but you have to try to use as little of them as possible.


Route question - El Forum - 04-06-2009

[eluser]tomcode[/eluser]
@xwero

Quote:Routes are great but you have to try to use as little of them as possible.

I totally agree with You. I'd build that kind of app around one main controller who catches all requests and then do all checks inside in one place. Lower mind blow factor.


Route question - El Forum - 04-06-2009

[eluser]xwero[/eluser]
That may be a more flexible solution but what you are doing is loading at least two controllers. Routing prevents this.


Route question - El Forum - 04-06-2009

[eluser]tomcode[/eluser]
One has to die one death,

Modular_Extensions_-_HMVC

Right now I am working on a system to have the logic packed into libraries


Route question - El Forum - 04-06-2009

[eluser]JulianM[/eluser]
I want to avoid having only 1 controller to make this thing.

I like @tomcode idea, however it would be ideal if there is any hack act as a kind of default controller if there is not other controller that match the criteria.

For example, taking in mind the www.domain.com/username pattern, same as found in many online applications such as twitter, etc. I guess it first check <username> not to be an existing controller or page (given that pages and controller may have higher priority than usernames).

I thought maybe a kind of hack can achieve this using this path:

1) Check if controller/page exists
2) If exists, then run that controller
3) If not exists (error 404) route to users controller to support www.domain.com/username

Thanks for your comments.


Route question - El Forum - 04-06-2009

[eluser]tomcode[/eluser]
Quote:a kind of default controller if there is not other controller that match the criteria.

That is exactly what You get when You use my example.

Try it !