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

Not for 'how-to' coding questions but PHP theory instead, this forum is here for those of us who wish to learn about design aspects of programming with PHP.

Moderator: General Moderators

Post Reply
User avatar
Luke
The Ninja Space Mod
Posts: 6424
Joined: Fri Aug 05, 2005 1:53 pm
Location: Paradise, CA

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

Post 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.
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Post 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.
(#10850)
User avatar
Luke
The Ninja Space Mod
Posts: 6424
Joined: Fri Aug 05, 2005 1:53 pm
Location: Paradise, CA

Post 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.
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Post 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.
(#10850)
User avatar
s.dot
Tranquility In Moderation
Posts: 5001
Joined: Sun Feb 06, 2005 7:18 pm
Location: Indiana

Post 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?
Set Search Time - A google chrome extension. When you search only results from the past year (or set time period) are displayed. Helps tremendously when using new technologies to avoid outdated results.
User avatar
Luke
The Ninja Space Mod
Posts: 6424
Joined: Fri Aug 05, 2005 1:53 pm
Location: Paradise, CA

Post by Luke »

Perhaps store page path info in the session?
I suppose that would work.
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Post 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.
(#10850)
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post 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?
Post Reply