CakePHP重定向路由 [英] CakePHP redirect routing

查看:176
本文介绍了CakePHP重定向路由的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的CakePHP 2.3应用程序中,我要 example.com/come/Harry 重定向 example.com/myworks/people/Harry

In my CakePHP 2.3 application, I want example.com/come/Harry to redirect example.com/myworks/people/Harry.

这样可以连接。

Router::connect ('/come/:myname',
    array('controller' => 'myworks', 'action' => 'people'),
    array(
        'pass' => array('myname')
    )
);

我需要301重定向。我尝试这个:

I need a 301 redirection. I tried this:

Router::redirect ('/come/:myname',
    array('controller' => 'myworks', 'action' => 'people'),
    array(
        'pass' => array('myname')
    )
);

但它重定向到 example.com/myworks/people / 。如何在重定向时将参数传递给我的操作?

But it redirected to example.com/myworks/people/. How can I pass argument to my action while redirecting ?

推荐答案

Per 您希望使用 persist 而不是传递用于重定向。此代码应该如您所愿工作:

Per the documentation you want to use persist rather than pass for redirects. This code should work as you want:

Router::redirect ('/come/*',
    array('controller' => 'myworks', 'action' => 'people',
          '?' => array('processed' => 1)),
    array(
        'persist' => array('myname')
    )
);

原因是因为您在重定向时生成了一个新网址:

The reason is because you're generating a new url when you redirect:



  • pass 用于定义哪些路由参数应该是
    数组。添加要传递的参数将从常规路由数组中删除它
    。例如, 'pass'=> array('slug')

  • persist
    用于定义应该自动包含哪些路由参数
    生成新网址。您可以通过
    重新定义持久化参数,或者通过将参数设置为
    false来删除它们。例如, 'persist'=> array('lang')

  • pass is used to define which of the routed parameters should be shifted into the pass array. Adding a parameter to pass will remove it from the regular route array. Ex. 'pass' => array('slug')
  • persist is used to define which route parameters should be automatically included when generating new urls. You can override persistent parameters by redefining them in a url or remove them by setting the parameter to false. Ex. 'persist' => array('lang')

这篇关于CakePHP重定向路由的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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