dmertl.com

authentication

Redirecting to requested URL in CakePHP with custom auth controller

August 20, 2009 by dmertl 5 Comments

I recently ran into the problem of having to redirect a user back to their requested url after logging in. In CakePHP you would normally use Auth::redirect() and that handles it for you. However, I had already written a custom authentication function in my app_controller. I simply wanted to figure out which url the user had requested, save that in the session, and send them back there after a successful login. This may seem like a very simple thing to do, but getting your current URL isn’t so easy in CakePHP.

The best solution I found was to use Router::getParams(). This will return you an array of info about the request. The currently request url, relative to you CakePHP install, will be in will be in [‘url’][‘url’]. The URL does not have a starting slash, unless the requested page was ‘/’. For example if you have CakePHP installed in http://www.mysite.com/cake_test/ and you request http:/www.mysite.com/cake_test/users/edit/15 the URL will be ‘users/edit/15’. So to add this into my auth controller I simply added:

$request_params = Router::getParams();
$this->Session->write('auth_redirect','/'.$request_params['url']['url']);

Then after a successful login you can simply:
$this->redirect($this->Session->read('auth_redirect'));

Posted in: CakePHP Tagged: authentication, CakePHP, redirect

Blogroll

  • Antarctica
  • Non-coding stuff
  • Photos
  • Rainbows

Recent Posts

  • Resizing optimized animated GIFs in Photoshop
  • Saving an animated GIF in Photoshop with “combine” instead of “replace” frames
  • Mimicking Django Admin Change Form Styling in a Custom Form
  • Installing custom Plex plugins on a Drobo 5N
  • Bug in CakePHP Containable Behavior, Related Records not Being Returned When Parent Contains a Single Field

Categories

  • CakePHP
  • Canon
  • Django
  • Doctrine
  • iPhone
  • mac
  • Magento
  • MS Access
  • MSSQL
  • Photography
  • Python
  • SQL
  • Uncategorized
  • Zend

Tags

@@IDENTITY adapter authentication breadcrumbs CakePHP Canon cocoa Doctrine email eos utility errors iPhone leopard lost cd mac Magento MS Access MS SQL mysql mysqli nosuchtable os x override parse pdo_mysql.php php programming raw redirect smtp SQL sqlite SQL Server triggers updater validation xcode Zend zend_mail_message

Copyright © 2023 dmertl.com.

Omega WordPress Theme by ThemeHall