Tuesday, December 25, 2012

Zend Framework 2 redirect and forward

Both uses different path name to identify.
Redirect uses physical url on browser side, therefore the route name is to be used.

For forward wise, its using the factory service name to identify the controller to run.
example:
config:

'controllers' => array(
        'invokables' => array(
            'Application\Controller\Index' => 'Application\Controller\IndexController',
            'Application\Controller\Login' => 'Application\Controller\LoginController'
        ),
    ),


'router' => array(
        'routes' => array(
            "login" => array(
                'type' => 'segment',
                'options' => array(
                    'route' => '/login[/:action][/]',
                    'constraints' => array(
                        'action' => '[a-zA-Z][a-zA-Z0-9_-]*',
                    ),
                    'defaults' => array(
                        'controller' => 'Application\Controller\Login',
                        'action' => 'index',
                    ),
                ),
            ),



$this->forward()->dispatch("Application\Controller\Index", array("action" => "someaction");

for redirect:
$this->redirect()->toRoute("login", array("action" => "someaction");

to get current controller name:
$this->getEvent()->getRouteMatch()->getParam("controller");

to get current route name:
return $this->getEvent()->getRouteMatch()->getMatchedRouteName();


No comments: