【问题标题】:Cakephp route with regex to render same view but with dynamic URL带有正则表达式的 Cakephp 路由以呈现相同的视图但具有动态 URL
【发布时间】:2016-01-02 21:26:32
【问题描述】:

我正在使用 Cakephp 2.0,并且我有一个控制器设置来接受动态 url 来呈现相同的视图:

http://domain/some-text-sid-2
http://domain/some-text-sid-3
.
.
.

页面的所有内容都被ajax相应地改变了。但是当我访问此页面或从其他页面重定向到此页面时,我想显示下面提到的链接中呈现的视图

http://domain/some-text

但网址应保持不变。例如,如果我访问页面:

http://domain/some-text-sid-2

那么视图应该是

http://domain/some-text

但 URL 不应更改。请提前帮助和感谢。

【问题讨论】:

  • 你读过本书on routing的部分吗?你尝试过什么样的路线?
  • @jeremyharris 是的,我已经阅读了路由文档。我还没有尝试过任何路线,因为我不知道如何实现。

标签: php regex cakephp cakephp-2.0


【解决方案1】:

一种可能的解决方案是将以下路由添加到/Config/routes.php

//Matches anything except URLs that include 'my_controller' to prevent looping
Router::redirect('/:text', array('controller' => 'my_controller', 'action' => 'my_action'), array('persist' => ['text'],'text'=>'(?!my_controller)(.*)'));

然后您在重定向后的操作中提取text

class MyController extends AppController 
{
    public function my_action() {
        $text = $this->params->named['text'];

        //[...] process action
    }
 }

【讨论】:

  • 谢谢@Inigo Flores,我工作了。我必须做一些工作,但你的指导帮助了我。再次感谢您。
猜你喜欢
  • 2012-04-30
  • 2017-08-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-11-06
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多