>/D_
Published on

CakePHP redirect to admin methods

Authors
  • avatar
    Name
    Frank
    Twitter

If you have admin routing set:

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

Redirecting between admin_* and non admin functions inside the controller, or linking to admin_* functions from non admin views can throw an error unless you pass an admin variable in the URL.

Redirecting to admin methods from the controller:
Inside the index() action if you redirect to another admin_*() action in the controller like:
$this->redirect(array('action' => 'admin_login'), null, true);

You get an error such as:

Error:  UsersController::admin_index() cannot be accessed directly.

Linking to an admin action from non admin view
webroot/users/

echo $html->link(__('Logout', true), array('controller'=> 'users', 'action'=>'admin_index'));

Errors with:

Error:  UsersController::admin_index() cannot be accessed directly.

To avoid this set the admin variable to the URL:

$this->redirect(array('action'=>'index', 'admin'=>1));
echo $html->link(__('Logout', true), array('controller'=> 'users', 'action'=>'admin_index'));

Kudos to the CakePHP google group for answering this question so promptly!