0

I'm using codeigniter framework and by default i load my site controller

$route['default_controller'] = "site";

But now i want to setup the routes.php config so i could still access my controllers as before but if controller doesent exists then i'd like to run users controller and check if it's nickname so i can display that users profile.

It's something like on Facebook where you can have www.facebook.com/username and it takes you to your user profile. But i'd like that my other controllers are still accessible www.mysite.com/site, www.mysite.com/site/function, etc

I have tried with :any wildcard but couldnt get it working. I have seen that some solve the problem with regex expression but i'm not good at regex and dont know how to put it together so it would work for me.

BoonZ
  • 443
  • 1
  • 6
  • 16
  • Your default url would then be `domain/site/userid` simply use match pattern and map `domain/userid` to `domain/site/userid` route. This way you can call `domain/1` and in the background CI will call site `controller > show` with that id. – Sterling Duchess Apr 02 '13 at 12:36

2 Answers2

2

Try my way:

First, Set $route for each controller that you have, exmp:

$route['controller_1'] = 'controller_1';
$route['controller_1/(:any)'] = 'controller_1/$1'; //this let you access your method
$route['controller_2'] = 'controller_2';
$route['controller_2/(:any)'] = 'controller_2/$1'; //this let you access your method

Next, use :any

$route['(:any)'] = 'user_controller/$1';

good luck !!!

egig
  • 4,370
  • 5
  • 29
  • 50
  • Hm... the concept is good but it doesent let me access controller_1/method_1 it only accesses controllers that are controller_1 or controller_2 without methods but i have a lot of methods in these controllers. So is there any other solution? – BoonZ Apr 02 '13 at 14:14
  • just editted my answer, now you can acces 'controller_1/method_1'. – egig Apr 02 '13 at 14:16
  • Yes that solves my problem but sometimes i also have id in url like controller_1/method_1/id will your update work with this or would i have to add $route['controller_1/(:any)/(:any)'] = 'controller_1/$1/$2'; ? – BoonZ Apr 02 '13 at 14:28
  • No, you wouldnt have to, my answer is enough. I tested it on mine and you also can test it on yours. – egig Apr 02 '13 at 14:31
0

Try this way. it will reduce a lot of repetitive line if you have lots of controller but i don't know does it violate any CI rules.

//this code block should be placed after any kind of reserved routes config
$url_parts = explode('/',strtolower( $_SERVER['REQUEST_URI']) );
$reserved_routes = array("controller_1", "controller_2", "controller_3" );
if (!in_array($url_parts[1], $reserved_routes)) {    
    $route['([a-zA-Z0-9_-]+)'] = "controller_1/profile/$1";
}
Atikul Islam
  • 1
  • 1
  • 2