【问题标题】:how to remove action name from URL in cakephp2如何从 cakephp2 中的 URL 中删除操作名称
【发布时间】:2013-07-22 06:32:38
【问题描述】:

可能是重复的,但我没有得到任何正确的答案或帮助

其实我也想做这样的事情:

我当前的网址是:http://mysite.com/MyController/view/page1

但我想要类似:http://mysite.com/MyController/page1

表示我想从 URL 中隐藏操作名称。

我用过

Router::connect('/:controller/:id',array('action' => 'view'),array('id' => '[0-9]+'));

但它不适合我

低于一个工作正常,但

Router::connect('/:controller/*', array('action' => 'view'),array('id' => '[0-9]+'));

它适用于所有控制器,但我想申请特定控制器

【问题讨论】:

  • 感谢参考。但我不知道如何做到这一点,所以请你帮帮我
  • 我用过Router::connect('/:controller/:id',array('action' => 'view'),array('id' => '[0-9] +'));但它不适合我
  • 进行这个小调整真的值得花时间和精力吗?我敢打赌有更大的鱼要炸..

标签: .htaccess cakephp routing routes cakephp-2.0


【解决方案1】:

使用

Router::connect('/MyController/:id', array('controller' => 'MyController','action' => 'view'),array('id' => '[0-9]+'));

【讨论】:

    【解决方案2】:

    你可以使用 Cake 的路由来让它工作。

    将以下内容添加到您的 app/Config/routes.php

    Router::connect('/Controller/page1', '/Controller/view/page1');
    

    但是您必须为每个“页面”添加一条路线。

    您可以使用通配符路由匹配以 /Controller/ 开头的所有内容:

    Router::connect('/Controller/*', '/Controller/view/');
    

    或者,不接触路线:

    class FooController extends AppController
    
        public function index($stub) {
    
            $data = $this->findByStub($stub);
    
            if (!$data) {
                die('page not found');
            }
    
            $this->set('data', $data);
    
        }
    
    }
    

    }

    这允许您拥有诸如 /foo/page1 之类的网址

    (例程查找具有匹配 'page1' 的存根字段的 Foo)

    这可行,但你会失去反向路由的好处,这意味着你可以像这样创建链接: $this->Html->link(array('controller'=>'foo', 'action'=>' view', 'page1'); 哪个蛋糕会自动重写生成:/foo/page1

    【讨论】:

    • 对不起哥们,它不适合我,或者做错了什么,我把你的索引函数放到我的控制器上,但它给了我错误,比如在控制器中找不到 page1 函数
    • 这个链接怎么样..stackoverflow.com/a/14070172/576523..我没试过这个..但这里也几乎同样的问题......
    【解决方案3】:

    使用此代码

    Router::connect('/:controller/*', array('action' => 'view'),array('id' => '[0-9]+'));
    

    【讨论】:

    • 但它适用于所有控制器,但我想适用于特定控制器
    【解决方案4】:

    为您的控制器尝试以下代码,这里以 GroupsController 为例

    你在你的 app\Config\routes.php 中添加这个

    Router::connect('/groups/:slugParam', array('controller' => 'groups', 'action' => 'index'), array('slugParam' => '[a-zA-Z0-9]+'));
    

    这应该重定向表单的所有请求

    http://www.site.com/groups/* 到 http://www.site.com/groups/index

    ( * => 控制器名称后面的任何内容)

    所以现在我必须更改 GroupsController 中的默认索引函数以反映此更改

    <?php
    App::uses('AppController', 'Controller');
    class GroupsController extends AppController {
    
            public function index($id = null) {
    
                //pr($this->request->params);   this where all data is intercepted...
                if(isset($this->request->params['slugParam']) && !empty($this->request->params['slugParam'])) {
    
                    // i have a slug field in groups databsae and hence instead of id am using slug field to identify the post.
                    $data = $this->Group->findBySlug($this->request->params['slugParam']);
                    $this->set('group', $data);
                    $this->render('/groups/view');
                } 
        }
    }
    ?>
    

    【讨论】:

    • 如果我使用 Router::connect('/groups/:slugParam', array('controller' => 'groups', 'action' => 'index'), 会遇到一个问题数组('slugParam' => '[a-zA-Z0-9]+'));它对我不起作用,但如果我将使用 Router::connect('/groups/:id', array('controller' => 'groups', 'action' => 'index'), array('slugParam' = > '[a-zA-Z0-9]+'));然后它的工作
    【解决方案5】:

    使用此代码

    Router::connect('/MyController/:id', array('controller' => 'MyController','action' => 'view'),array('id' => '[0-9]+'));
    

    【讨论】:

      猜你喜欢
      • 2013-07-22
      • 1970-01-01
      • 1970-01-01
      • 2020-09-09
      • 1970-01-01
      • 1970-01-01
      • 2016-07-24
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多