从重定向的请求中获取params

I'm trying to redirect the user using this code:

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

And getting from the layout the content of the e param this way:

$_REQUEST['e']

But doing that I don't catch anything. How do I to get it please?

Thanks in advance!

php
dongpanbo4727
dongpanbo4727 然后,您没有正确地将var发送到视图。在发送到视图之前将print_r()放在控制器中。如果这不起作用,请使用此控制器/操作的路由配置和控制器代码制作一个pastebin或粘贴您的代码。
6 年多之前 回复
dqz7636
dqz7636 它也不起作用。我只是使用die(print($this->e));从视图中测试它。我已经测试了$_REQUEST[“e”],$this->e和$e。
6 年多之前 回复
dongzhong5573
dongzhong5573 我正在从控制器向视图发送参数。
6 年多之前 回复
doufang3001
doufang3001 您可以从控制器发送它或创建自己的视图助手
6 年多之前 回复
duanliang8464
duanliang8464 我试图从视角来处理这个问题
6 年多之前 回复
duanshan3065
duanshan3065 $this->params()->fromRoute('e',0);在控制器中
6 年多之前 回复

2个回答

Matched Route params in view :

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

Request Query/Post in view :

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

$this->getHelperPluginManager()
->getServiceLocator()
->get('Request')
->getPost()->toArray()
doubu1970
doubu1970 我不这么认为,学会调试,女巫部分不工作?
6 年多之前 回复
duanlaofu4108
duanlaofu4108 这是行不通的,你需要以前的配置吗?
6 年多之前 回复

As mentioned in the comments of your question, the way to go is: $this->params()->fromRoute(); mentioned by @Notuser. Will use it in a simple example for your use as you're passing the parameters to the view.

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);
    }
}

So in your view.phtml you can now easily do <?php echo $this->e; ?> which should contain: n.

The route.config of the above example will look something like this:

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'
                    )
                )
            )
        )
    )
);
Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问
相关内容推荐