Page 1 of 1

Zend Framework - a "back to where you came from" p

Posted: Thu Oct 04, 2007 11:08 am
by Luke
I am trying to come up with the best way (within the Zend Framework) to keep whatever page you request, and then if you are not logged in, it saves that url you requested, and once you login, it sends you back where you were. I kind of figured this is what controller plugins are for... I already have an Auth plugin that basically detects if you are authenticated and authorized to view whatever you're requesting, and if you aren't it boots you to the login screen. Seems like the perfect place to put it... but as far as design goes, I'm not sure that this is where it belongs. Should I write a second plugin?

Also, even if/when I do write the plugin, I'm sort of unsure how to go about it. The controller plugins thing has got me a little confused, so I'm just looking for advice.

Posted: Thu Oct 04, 2007 11:22 am
by Christopher
Just have your redirect pass the path and params to your login page. Then add logic to the login page to redirect if it received parameters.

Posted: Thu Oct 04, 2007 11:37 am
by Luke
Oh... I guess I was just thinking that there was a way (with the postDispatch and preDispatch stuff) to do all that without really having to do much/any modification to my controllers.

EDIT: It just seems to me that logic belongs "above" any specific controller.

Posted: Thu Oct 04, 2007 1:17 pm
by Christopher
Well the "is not logged in" logic should be in the Front Controller, but the Login Controller can then have the smarts to do the right thing if it is given information need to forward after login.

Posted: Thu Oct 04, 2007 1:27 pm
by s.dot
Right.. but say you're using a post-redirect-get pattern. How does the controller determine the logic in this situation?

Perhaps store page path info in the session?

Posted: Thu Oct 04, 2007 5:12 pm
by Luke
Perhaps store page path info in the session?
I suppose that would work.

Posted: Thu Oct 04, 2007 6:16 pm
by Christopher
Because the relationship is only between the Access Control redirector and the Authentication page, you are pretty free to use whatever works without side effects.

Posted: Thu Oct 04, 2007 10:53 pm
by John Cartwright

Code: Select all

<?php

require_once 'Zend/Session/Namespace.php';

class Zodiac_Plugin_AuthRedirect extends Zend_Controller_Plugin_Abstract
{ 
	public function __construct()
	{
		$this->_session = new Zend_Session_Namespace('authenticate');
	}
	
	public function preDispatch($request)
	{
		//perform the authentication here
		if (!$this->_session->authenticated) {

		}
	}
	
	public function postDispatch($request)
	{
		$controller = $request->getControllerName();
		
		//if we have a url to redirect to, is authenticated and is still on the login controller
		if ($this->_session->lastRequest && $this->_session->authenticated && $controller == 'login') {
			$redirector = Zend_Controller_Action_HelperBroker::getStaticHelper('redirector');
			$redirector->setGotoUrl($this->_session->lastRequest); 		
		}
		//store the last url excluding those when on the login page
		elseif ($controller != 'login' && !$this->_session->authenticated) {
			$this->_session->lastRequest = $request->getRequestUri();
		}	
	}
}

?>
I am interested in the idea of applying this to a plugin, since I'm trying to push as much away from the controller as possibly as of lately. Heres a kind of how I would attempt such a thing. Some things to note is that use of the redirector helper in a plugin is a known issue.

Thoughts?