Zend Framework MVC redirecting to different controller and action
Asked Answered
P

3

5

i'm trying to work out how i can stop zend or redirect zend to go to a different zend controller and action if a check within the boot strap fails.

for example a get variable does not exist or more likely a session does not exist meaning the user must log in.

so the user had originally requested index/someaction

but i want them to go to login/index

within my boot strap i would place the condition and then change the controller action to view.

if i'm doing this in a way that is not standard can anyone direct me to documentation on the best practice ?

zend novice

Patterman answered 26/10, 2010 at 14:12 Comment(0)
G
4

From Zend documentation (Dispatcher)

// forward to an action in another controller:
// FooController::bazAction(),
// in the current module:
$this->_forward('baz', 'foo', null, array('baz' => 'bogus'));
Garrettgarrick answered 26/10, 2010 at 14:47 Comment(0)
H
4

I'd sugest you to do with plugins for access check on each page and for login create an authentication controller.

Here you'll find out how to do this http://alex-tech-adventures.com/development/zend-framework/61-zendauth-and-zendform.html

An example:

class Plugin_AccessCheck extends Zend_Controller_Plugin_Abstract 
{
    public function preDispatch(Zend_Controller_Request_Abstract $request)
    {
        // ...

        if(!$auth->hasIdentity())
        {
            $request->setControllerName('authentication')
                 ->setActionName('login');
        }
    }
}
Huckster answered 26/10, 2010 at 17:20 Comment(0)
H
2

I don't usually put authentication in the bootstrap, that should have it's own controller.

Create an AuthController() to set up your auth adapter and and set up your instance.

Then in a common view (for secure pages), just check your instance with something like:

$auth = Zend_Auth::getInstance();     
if(!$auth->hasIdentity())      
{
#re-direct to login page
}
Handily answered 26/10, 2010 at 14:45 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.