Why are my params not showing up in the url?

2019-08-10 06:20发布

问题:

Why are my action and params not showing up in my url? I am using the zend 2 framework. I have a search action and a results action. Below is my search action (not using variables as I test):

        return $this->forward()->dispatch('Application\Controller\Index', array(
        'action'     => 'results',
        'zip'   => '12345',
      ));   

The route is a child of my home route.

            'results' => array(
                'type' => 'segment',
                'options' => array(
                    'route' => 'results[/:zip]',
                    'defaults' => array(
                        'controller' => 'Application\Controller\Index',
                        'action'     => 'results',
                    ),
                ),
            ),

Below is the parent route

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

I am getting routed to the correct view but the url returns www.foo.com as opposed to www.foo.com/results/12345.

In addition how can I stop the post request on a forward? There is a form on the results page and after the forward this form is again submitted (causing a loop).

回答1:

If you want the browser to display the new URL then you need to perform a HTTP redirect as the forward plugin will dispatch to the new controller in the same request.

Replace the forward call with a call to the redirect plugin.

return $this->redirect()->toRoute('home/results/route/name');

This will also solve your addition issue where the HTTP method is still set as POST (as the redirect will be a GET request).