为什么我的参数没有显示在 url 中? [英] Why are my params not showing up in the url?

查看:42
本文介绍了为什么我的参数没有显示在 url 中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为什么我的操作和参数没有显示在我的网址中?我正在使用 Zend 2 框架.我有一个搜索操作和一个结果操作.以下是我的搜索操作(我测试时不使用变量):

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',
                    ),
                ),
            ),

下面是父路由

'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',
                ),
            ),

我被路由到正确的视图,但 URL 返回 www.foo.com 而不是 www.foo.com/results/12345.

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

另外,我怎样才能停止转发的 post 请求?结果页面上有一个表单,转发后再次提交此表单(导致循环).

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).

推荐答案

如果您希望浏览器显示新的 URL,那么您需要执行 HTTP 重定向,因为转发插件将分派到 中的新控制器相同的请求.

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

这也将解决您的添加问题,即 HTTP 方法仍设置为 POST(因为重定向将是 GET 请求).

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

这篇关于为什么我的参数没有显示在 url 中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆