I am just getting started with ZF2 (now that people are talking about ZF3
(-: ) and wondering what the recommended practice is in the following case.
I have a module where I want to require the user be logged in to access any
controller/action under this module, otherwise redirect to a login page (in
another module).

I have experimented with attaching a listener to the 'dispatch' event in my
Module.php's onBootstrap method. Something like :

public function onBootstrap(MvcEvent $e)
    {
       /* .......*/

       $eventManager->attach('dispatch', function() use ($e,
$authenticated) {

            if (! $authenticated) {
                $target = $e->getTarget();
                if ($target instanceof
\Zend\Mvc\Controller\AbstractActionController) {
                    if ( 0 === strpos(get_class($target),'MyModule')) {
                        $target->redirect()->toRoute('login');
                    }
            }
    }

Is this basically the right approach? Or is there a better way to go at
this?

Thanks.

-- 
David Mintz
http://davidmintz.org/
Human needs before private profit:
http://socialequality.com/

Reply via email to