如何在CakePHP中使用自定义路由?

时间:2016-11-25 02:58:12

标签: php cakephp routing cakephp-2.0 custom-routes

我想在CakePHP中实现自定义路由。我正在关注文档

http://book.cakephp.org/2.0/en/development/routing.html#custom-route-classes

app/Routing/Route

中的自定义路线
<?php

App::uses('CakeRoute', 'lib/Cake/Routing/Route');

class CategoryRoute extends CakeRoute
{
    public function parse($url)
    {
        $params = parent::parse($url);
        if (empty($params)) {
            return false;
        }

        return true;
    }
}

app/Config/routes.php

App::uses('CategoryRoute', 'Routing/Route');
Router::connect('/mypage/*', array('controller' => 'mycontroller', 'action' => 'view'), ['routeClass' => 'CategoryRoute']);

但我得到

Missing Controller

Error: Controller could not be found.

Error: Create the class Controller below in file: app/Controller/Controller.php

当我删除['routeClass' => 'CategoryRoute']时,重新路由工作正常。

1 个答案:

答案 0 :(得分:1)

仔细查看API文档: API > CakeRoute::parse()

parse()方法应该在成功时返回一个已解析参数的数组(即$params),或者在失败时返回false