CakePHP redirect to admin methods

by frank on July 30, 2008

in Tips of the Week

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!

Was this article useful?

rss feed icon

Email this article to yourself or...

rss feed icon

Subscribe to the RSS feed for more useful articles and tips.

Share this article with others

  • del.icio.us
  • Twitter
  • Reddit
  • StumbleUpon
  • Facebook
  • Digg
  • http://blogfreakz.com mupet

    thanks bro, this article usefull for me

  • yoesoff

    good article.

  • http://deadlytechnology.com/scripts/php/api-cakephp-app/ API for cakePHP apps Part 1: The Problems | Deadly Technology

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

  • marcoarruda

    Thanks, very helpful

  • http://www.asdf.com asdf

    Nice Article. really helped a lot!!!
    thanks

  • saqi

    When i type http://localhost/csample/zee/admin/
    it redirects me on webroot. folder on the url http://localhost/csample/zee/app/webroot/admin/

    any solution ?

  • frank

    sorry saqi, bit hard to debog that one from here :-s I would try the cakephp IRC channel.