{} - 3.4.5.5 Prefix Routing

Many applications require an administration section where priveledged users can make changes. This is often done through a special URL such as /admin/users/edit/5. In CakePHP, admin routing can be enabled from within the core configuration file by setting the admin path for Routing.admin.

Configure::write('Routing.admin', 'admin');
  1. Configure::write('Routing.admin', 'admin');

In your controller, any action with an admin_ prefix will be called. Using our users example, the method name of our UsersController would be admin_edit.

You can use the Router to use custom prefixes for uses beyond admin routing, too.

Router::connect('/profiles/:controller/:action', array('prefix' => 'profiles')); 
  1. Router::connect('/profiles/:controller/:action', array('prefix' => 'profiles'));

Any calls to the profiles section would look for the profiles_ prefix on the method calls. Our users example would have a URL structure that looks like /profiles/users/edit/5 would call the profiles_edit method within the UsersController. Also important to remember, using the HTML helper to build your links will help maintain the prefix calls. Here's an example link being built using the HTML helper:

echo $html->link('Edit your profile', array('controller' => 'users', 'action' => 'profiles_edit')); 
  1. echo $html->link('Edit your profile', array('controller' => 'users', 'action' => 'profiles_edit'));

You can set up multiple prefixed routes using this approach to create a flexible URL structure for your application.

{} - 3.4.5.5 Prefix Routing

Many applications require an administration section where priveledged users can make changes. This is often done through a special URL such as /admin/users/edit/5. In CakePHP, admin routing can be enabled from within the core configuration file by setting the admin path for Routing.admin.

Configure::write('Routing.admin', 'admin');
  1. Configure::write('Routing.admin', 'admin');

In your controller, any action with an admin_ prefix will be called. Using our users example, the method name of our UsersController would be admin_edit.

You can use the Router to use custom prefixes for uses beyond admin routing, too.

Router::connect('/profiles/:controller/:action', array('prefix' => 'profiles')); 
  1. Router::connect('/profiles/:controller/:action', array('prefix' => 'profiles'));

Any calls to the profiles section would look for the profiles_ prefix on the method calls. Our users example would have a URL structure that looks like /profiles/users/edit/5 would call the profiles_edit method within the UsersController. Also important to remember, using the HTML helper to build your links will help maintain the prefix calls. Here's an example link being built using the HTML helper:

echo $html->link('Edit your profile', array('controller' => 'users', 'action' => 'profiles_edit')); 
  1. echo $html->link('Edit your profile', array('controller' => 'users', 'action' => 'profiles_edit'));

You can set up multiple prefixed routes using this approach to create a flexible URL structure for your application.

Differences