从重定向的请求中获取params

时间:2014-07-16 12:20:24

标签: php zend-framework2

我尝试使用此代码重定向用户:

return $this->redirect()->toRoute('application', array(
                    'controller' => 'Index',
                    'action' => 'connexion',
                    null,
                    array('e' => 'n'),
                ));

以这种方式从布局中获取e param的内容:

$_REQUEST['e']

但这样做我什么都抓不到。我该怎样才能得到它?

提前致谢!

2 个答案:

答案 0 :(得分:0)

视图中匹配的路线参数:

$this->getHelperPluginManager()
    ->getServiceLocator()
    ->get('Application')
    ->getMvcEvent()
    ->getRouteMatch()
    ->getParams()

请求查询/发布视图:

$this->getHelperPluginManager()
->getServiceLocator()
->get('Request')
->getQuery()->toArray()

$this->getHelperPluginManager()
->getServiceLocator()
->get('Request')
->getPost()->toArray()

答案 1 :(得分:0)

正如您的问题评论中所提到的,要走的路是:@Notuser提到的$this->params()->fromRoute();。将它用在一个简单的示例中供您使用,因为您将参数传递给视图。

class ExampleController extends AbstractActionController
{

    public function rerouteAction()
    {
        // Notice that 'param' is a route within our route.config.php and in there we
        // define the controller and action, so we do not need to set the controller
        // and action in the redirect. So param now points to paramAction of ExampleController.
        return $this->redirect()->toRoute('param', array('e' => 'n'));
    }

    public function paramAction()
    {
        // Leaving fromRoute() blank will return all params!
        $params = $this->params()->fromRoute();
        $e = $params['e'];
        return array('e' => $e);
    }
}

因此,在您的view.phtml中,您现在可以轻松地执行<?php echo $this->e; ?>,其中应包含:n

以上示例的route.config将如下所示:

return array(
    'router' => array(
        'routes' => array(
            'reroute' => array(
                'type' => 'segment',
                'options' => array(
                    'route' => 'reroute',
                    'defaults' => array(
                        'controller' => 'Application\Controller\ExampleController',
                        'action' => 'reroute'
                    )
                )
            ),
            'param' => array(
                'type' => 'segment',
                'options' => array(
                    'route' => 'param',
                    'defaults' => array(
                        'controller' => 'Application\Controller\ExampleController',
                        'action' => 'param'
                    )
                )
            )
        )
    )
);