Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/262.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何配置cakephp自定义路由类_Php_Cakephp_Url Routing - Fatal编程技术网

如何配置cakephp自定义路由类

如何配置cakephp自定义路由类,php,cakephp,url-routing,Php,Cakephp,Url Routing,我已经在CakePHP2.x中创建了自定义路由器类,我只是在关注博客文章。在我的应用程序中,我没有/Routing/Route文件夹,我创建文件夹并将StaticSlugRoute.php文件放入其中。在该文件中包括以下代码 <?php App::uses('Event', 'Model'); App::uses('CakeRoute', 'Routing/Route'); App::uses('ClassRegistry', 'Utility'); class StaticSl

我已经在CakePHP2.x中创建了自定义路由器类,我只是在关注博客文章。在我的应用程序中,我没有/Routing/Route文件夹,我创建文件夹并将StaticSlugRoute.php文件放入其中。在该文件中包括以下代码

<?php
 App::uses('Event', 'Model');
 App::uses('CakeRoute', 'Routing/Route');
 App::uses('ClassRegistry', 'Utility');

 class StaticSlugRoute extends CakeRoute {

    public function parse($url) {
        $params = parent::parse($url);
        if (empty($params)) {
            return false;
        }
        $this->Event = ClassRegistry::init('Event'); 
        $title = $params['title']; 
        $event = $this->Event->find('first', array(
                    'conditions' => array(
                        'Event.title' => $title,
                    ),
                    'fields' => array('Event.id'),
                    'recursive' => -1,
                    ));
        if ($event) {
            $params['pass'] = array($event['Event']['id']);
            return $params;
        }
        return false;
    }
}

?>


我添加了此代码,但它似乎不起作用(事件/索引工作正常)。我想将“www.example.com/events/event title”url路由到“www.example.com/events/index/id”。是否有任何东西我遗漏或我需要导入此代码到任何地方。如果可以重定向此类('www.example.com/event title')url。

自定义路由类应位于/Lib/Routing/route内,而不是/Routing/route内

然后需要在routes.php文件中导入自定义类

 App::uses('StaticSlugRoute', 'Lib/Routing/Route');
 Router::connect('/events/:slug', array('controller' => 'events', 'action' => 'index'), array('routeClass' => 'StaticSlugRoute'));
这告诉CakePhp对类似于/events/:slug(ex:/events/event title)的URL使用自定义路由类


旁注:不要忘记为适当的数据库字段建立索引,以避免在行数增加时严重影响性能。

自定义路由类应位于/Lib/Routing/route内,而不是/Routing/route内

然后需要在routes.php文件中导入自定义类

 App::uses('StaticSlugRoute', 'Lib/Routing/Route');
 Router::connect('/events/:slug', array('controller' => 'events', 'action' => 'index'), array('routeClass' => 'StaticSlugRoute'));
这告诉CakePhp对类似于/events/:slug(ex:/events/event title)的URL使用自定义路由类


旁注:不要忘记为适当的数据库字段编制索引,以免在行数增加时严重影响性能。

为什么不让事件/索引采用事件标题而不是事件id?然后,您可以使用该逻辑在events/index中查找事件,并在config/routes.php中使用普通配置,而不是编写自己的路由类。感谢您的想法:)但我已经在“config/routes.php”中做了“www.example.com/events/event title”这件事了。现在我必须添加一些静态页面(关于我们,请与我们联系……未来将有更多页面。-www.example.com/about-us),现在需要路由到“www.example.com/event-title”URL,有时这与静态页面路由冲突。因此,它需要自定义到事件页面或静态页面的路由。为什么不让事件/索引采用事件标题而不是事件id?然后,您可以使用该逻辑在events/index中查找事件,并在config/routes.php中使用普通配置,而不是编写自己的路由类。感谢您的想法:)但我已经在“config/routes.php”中做了“www.example.com/events/event title”这件事了。现在我必须添加一些静态页面(关于我们,请与我们联系……未来将有更多页面。-www.example.com/about-us),现在需要路由到“www.example.com/event-title”URL,有时这与静态页面路由冲突。所以它需要定制到事件页面或静态页面的路由;更适合2.xYou还可以使用App::uses('StaticSlugRoute','Lib/Routing/Route');更适合2.x