SEO, PHP and Javascript Web Dev

Search Engine Optimisation, Web Development and Network Administration Ramblings

-->
30  07 2008

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 this Post:
  • Reddit
  • Sphinn
  • del.icio.us
  • Digg
  • e-mail
  • Mixx
  • Google
  • StumbleUpon

Related posts:

  1. Tip of the Week : Use a fallback AJAX failure mechanism
  2. CakePHP paginator for multiple sets of data on one page using AJAX
  3. Tip of the Week : Nested Ternary Statement PHP
  4. Restict Logins by IP Address
  5. Translate a nested heirarchical array into another heirarchical array

Tags: cakephp, PHP, tip
« Geek Must Have : Autonomous Paintball Sentry
Comparing web development frameworks by Opportunity »

Leave a Reply

-->
  • Photography