Friday, October 5, 2012

How To Properly Redirect Using Zend Framework 2

There are two things you need to do to properly redirect in Zend Framework 2. When I say proper, that is redirecting to pretty URL without hardcoding the path and instead use a route reference which you can go back to and change to your liking in the future. To do so, you must.

1. Define the route.

On your config, either local.php, global.php or module.config.php, write the following code,

'your_route' => array(
                                  'type' => 'Literal',
                                  'options' => array(
                                      'route' => '/your/route',
                                      'defaults' => array(
                                          '__NAMESPACE__' => 'Your\Controller',
                                          'controller' => 'Route',
                                          'action' => 'index',
                                      ),
                                  ),
                                  .... some more routing options
)

2. Call the route.

On your module->controller->action where you want to redirect to 'your_route', write the following,

return $this->redirect()->toRoute('your_route');
  • Related Links Widget for Blogspot

No comments: