Skip to content


Tip of the Week : CakePHP redirect to admin methods

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!

Share and Enjoy:
  • Digg
  • Sphinn
  • del.icio.us
  • Facebook
  • Mixx
  • Google Bookmarks
  • email
  • Fark
  • Reddit
  • StumbleUpon

Profile:  Frank has been programming for the web using PHP, Javascript and numerous libraries and frameworks for the past 5 years. More articles.

Posted in Tips of the Week. Tagged with cakephp, PHP, tip.

4 Responses

Stay in touch with the conversation, subscribe to the RSS feed for comments on this post.

  1. thanks bro, this article usefull for me

  2. good article.

  3. Thanks, very helpful

Continuing the Discussion

  1. API for cakePHP apps Part 1: The Problems | Deadly Technology linked to this post on April 29, 2010

    [...] simple, essentially the same as admin routing. So for an example [...]

Some HTML is OK

(required)

(required, but never shared)

or, reply to this post via trackback.