How to set query parameter with redirect()

2019-05-27 11:38发布

I would like to set a query parameter when redirecting. I tried this:

$this->redirect()->toRoute('login/default', array('action' => 'forgotPassword', 'foo' => 'bar'));

It redirects to:

/login/forgotPassword

Instead of where I would like to redirect which is:

/login/forgotPassword?foo=bar

2条回答
干净又极端
2楼-- · 2019-05-27 12:16

The query parameter belongs to the third parameter of the URL-Methods.

$this->redirect()->toRoute(
    'login/default', 
    array(
        'action' => 'forgotPassword'
    ),
    array( 'query' => array(
        'foo' => 'bar'
    ))
)
查看更多
Fickle 薄情
3楼-- · 2019-05-27 12:27

Plus.

To redirect a "access" or login, form you can use:

if (!$controller->identity()) {
  $sm = $controller->getServiceLocator();
  $router = $sm->get('router');
  $request = $sm->get('request');

  $routeMatch = $router->match($request);

  $controller->redirect()->toRoute('login', array(), 
        array( 'query' => 
                array('redir' => $routeMatch->getMatchedRouteName() ) ) );
}

Urls will be: /login/?redir=current-route

查看更多
登录 后发表回答