.htaccess 如何从cakephp2中的URL中删除操作名称

.htaccess 如何从cakephp2中的URL中删除操作名称,.htaccess,cakephp,routing,routes,cakephp-2.0,.htaccess,Cakephp,Routing,Routes,Cakephp 2.0,可能是重复的,但我没有得到任何正确的答案或帮助 其实我想做的是: 我当前的URL是: 但我想要的是: 表示我想从URL中隐藏操作名称 我用过 Router::connect('/:controller/:id',array('action' => 'view'),array('id' => '[0-9]+')); 但这对我不起作用 低于1工作正常,但 Router::connect('/:controller/*', array('action' => 'view'),arr

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

其实我想做的是:

我当前的URL是:

但我想要的是:

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

我用过

Router::connect('/:controller/:id',array('action' => 'view'),array('id' => '[0-9]+'));
但这对我不起作用

低于1工作正常,但

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

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

您可以使用Cake的路由来实现这一点

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

Router::connect('/Controller/page1', '/Controller/view/page1');
Router::connect('/groups/:slugParam', array('controller' => 'groups', 'action' => 'index'), array('slugParam' => '[a-zA-Z0-9]+'));
但您必须为每个“页面”添加一个路由

可以使用通配符路由匹配以/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之类的URL

(例程查找存根字段与“page1”匹配的Foo)

这是可行的,但您将失去反向路由的好处,这意味着您可以创建如下链接:$This->Html->link(数组('controller'=>'foo','action'=>'view','page1');哪个蛋糕将自动重写以生成:/foo/page1

使用此代码

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


为您的控制器尝试以下代码,这里我使用GroupsController作为示例

您可以将其添加到app\Config\routes.php中

Router::connect('/Controller/page1', '/Controller/view/page1');
Router::connect('/groups/:slugParam', array('controller' => 'groups', 'action' => 'index'), array('slugParam' => '[a-zA-Z0-9]+'));
这将重定向表单的所有请求

*到

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

因此,现在我必须修改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('/:controller/*', array('action' => 'view'),array('id' => '[0-9]+'));
Router::connect('/MyController/:id', array('controller' => 'MyController','action' => 'view'),array('id' => '[0-9]+'));

谢谢mark的参考。但我不知道如何做,所以请您帮助我使用路由器::connect('/:controller/:id',数组('action'=>'view'),数组('id'=>'[0-9]+'));但它对我不起作用是不是真的值得花时间和精力来做这个小小的调整?我打赌还有更重要的事情要做..对不起,伙计,它对我不起作用,或者我做错了什么,我把你的索引函数放在我的控制器上,但它给了我错误,比如在cotrollerHow中找不到关于这个链接的page1函数..我还没有找到tri我在这里遇到了一个问题,如果我使用路由器::connect('/groups/:slugParam',array('controller'=>'groups','action'=>'index'),array('slugParam'=>'[a-zA-Z0-9]+')),我在这里遇到了一个问题;它对我不起作用,但如果我使用Router::connect('/groups/:id',数组('controller'=>'groups','action'=>'index'),数组('slugParam'=>'[a-zA-Z0-9]+');它起作用了