codeigniter get all declared routes
Asked Answered
K

3

10

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';
Keslie answered 15/11, 2012 at 3:19 Comment(0)
B
25

From Controller you can do this

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

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

Beach answered 15/11, 2012 at 3:39 Comment(0)
D
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;
}
Dibri answered 15/11, 2012 at 3:53 Comment(0)
Y
0

In a console, at root of your project, you can get all routes with the command :

php spark routes

This will show you someting like :

+-----------+-------------------------+---------------+-----------------------------------+----------------+---------------+
| Method    | Route                   | Name          | Handler                           | Before Filters | After Filters |
+-----------+-------------------------+---------------+-----------------------------------+----------------+---------------+
| GET(auto) | product/list/../..[/..] |               | \App\Controllers\Product::getList |                | toolbar       |
+-----------+-------------------------+---------------+-----------------------------------+----------------+---------------+

See Documentation

Yate answered 8/9, 2023 at 9:11 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.