hemppa
hemppa

Reputation: 209

CakePHP administrator routing

I am having a hard time to understand this admin-routing of CakePHP. From the Cookbook:

"You can map the url /admin to your admin_index action of pages controller using following route:"

Router::connect('/admin', array('controller' => 'pages', 'action' => 'index', 'admin' => true));

What is not clear to me is the "'admin' = true". What is the purpose of this? When you call logout-function, do you pass the argument "'admin' = false" to redirect function in AppController?

When you have multiple prefixes in use, do you use "'manager' = true" if you want to enable manager-prefix?

Upvotes: 1

Views: 5747

Answers (2)

RichardAtHome
RichardAtHome

Reputation: 4313

admin=true in a route means your url is prefixed with /admin.

So in your example, your /admin route actually connects to: /admin/pages/index, and is serviced by the admin_index() action in your pages controller (as opposed to the non-prefixed index() action).

You can either ensure all links to logout are created with admin=false so they map to the standard Users::logout() action, or create a new action admin_logout() which handles admin logouts.

Adding manager=true to a url (along with the associated prefix setting) means the url with begin with /manager, and will map to manager_...() functions in the controller.

You can use both (or more!) prefixes, but not in the same url.

/pages/index maps to: PagesController:index();

/admin/pages/index maps to: PagesController:admin_index();

/manager/pages/index maps to: PagesController:manager_index();

Upvotes: 4

Reactgular
Reactgular

Reputation: 54741

Router Prefixes

In CakePHP 2.x prefixes changed a little.

You can have multiple prefixes now, but they have to be declared in the core.php file.

Configure::write('Routing.prefixes', array('admin','api','json'));

That would declare 3 prefixes, and there is no need to modify the routing tables to make them work. The word prefix means it's placed before the name of the action when dispatching to the controller.

For example;

class DocumentsController extends AppController
{
     public index() { ... }
     public admin_index() { ... }
     public api_index() { ... }
     public json_index() { ... }
}

CakePHP will call the correct action when one of these URLs are request.

http://example.com/documents/index
http://example.com/admin/documents/index
http://example.com/api/documents/index
http://example.com/json/documents/index

What you can not do is use more than one prefix at a time. The following will NOT work.

http://example.com/admin/json/documents/index

That would require custom routing, because CakePHP doesn't know which action to call.

When an action is called you can which for prefixes in the request params.

public function beforeFilter()
{
   if(isset($this->request->params['admin']))
   {
        // an admin prefix call is being made
   }
}

Upvotes: 2

Related Questions