codeigniter get all declared routes

爷,独闯天下 提交于 2019-12-05 22:54:39

问题


How to get all declared routes in codeigniter? like ex. print_r($route)

Because this is the problem, if the customer registered his username as 'facebook' he will be routed to account/facebook_login and not to his profile, if i changed the order of routes, all links will be routed to customer/profile and this is a no no!

So basically, instead of listing all the routes that i declare and put it into an another array or db table, i want to loop into route array and check if there is a word that has been declared already so that i can stop them to register that word as their username.

this is my sample routes:

// Account routes
$route['login'] = 'account/login';
$route['logout'] = 'account/logout';
$route['register'] = 'account/register';
$route['facebook'] = 'account/facebook_login';
$route['twitter'] = 'account/twitter_login';
$route['settings'] = 'account/settings';

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

// Dynamic routes
$route['(:any)'] = 'customer/profile/$1';

回答1:


From Controller you can do this

print_r($this->router->routes);

It will show all the routes defined in routes.php.




回答2:


First of all I'm sorry for my English because "I am NOT SCHOOL". I didn't get much what you are trying to point out. Maybe you want to do similar with this http://www.hirepinoy.com/born2code. But in my experience with CodeIgniter,it is a bad idea to declare $route['(:any)'] = 'customer/profile/$1'; in your routes.

I think the best option you can do is that to create a class to check if username exist in the table of users by using HOOK see http://codeigniter.com/user_guide/general/hooks.html. So therefore when username (unique field) returned then you can modify the $_SERVER['REQUEST_URI'] to be like this

$_SERVER['REQUEST_URI'] = '/customer/profile/'.$username;

So basically it will modify the SERVER REQUEST before the codeigniter core process it's core processing.

Now, the problem maybe, is when user registered a username that is the same with your controller for sure will not be process since it was modified to route on costumer/profile/blahblah. All you need to do is to create a custom validation to check weather the username already exists on database and or your controller name.

You can do like

if (file_exists(APPPATH."controllers/{$value}.php")) {
    $this->CI->form_validation->set_message('is_unique', 'Username is already taken');  
    return FALSE;
}


来源:https://stackoverflow.com/questions/13391075/codeigniter-get-all-declared-routes

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!