CakePHP捕获所有路线

时间:2015-01-06 18:40:48

标签: php cakephp routing

我遇到了一个问题,我确信这只是因为我对蛋糕生锈了。我正在尝试创建一个默认或全能路由,只有在所有其他路由都失败时才会匹配。我从大多数MVC框架的理解中假设以下内容就足够了:

Router::connect('/', array('controller' => 'pages', 'action' => 'display', 'home'));
#... other routes
Router::connect('/*', array('controller' => 'pages', 'action' => 'dynamic_display', 'home'));
CakePlugin::routes();
require CAKE . 'Config' . DS . 'routes.php';

问题在于Router::connect('/*')路由导致与先前路由冲突。我也试过了一个“slug”路线,但我对冲突也有同样的问题。

这个问题是否有任何解决方案或可能的解决方法?

提前致谢。

修改

在下面的评论中,我链接到一条评论,为我的问题提供了一个不错的解决方案。这是我简单的概念证明。

page.php文件

<?php
App::uses('AppModel', 'Model');

/**
 * Page model
 * @uses AppModel
 */
    class Page extends AppModel {
        /**
         * MongoDB Schema definitions
         *
         * @var array 'mongoSchema'
         * @link https://github.com/ichikaway/cakephp-mongodb/
         */
            var $mongoSchema = array(
                'title'=>array('type'=>'string'),
                'meta_description'=>array('type'=>'text'),
                'slug'=>array('type'=>'string'),
                'content'=>array('type'=>'text'),
                'published'=>array('type'=>'bool'),
                'created'=>array('type'=>'datetime'),
            );

        /**
         * afterSave method. 
         * @return void
         */
            public function afterSave($created, $options=array()) {
                $this->__rebuildRouteCache();
            }

        /**
         * rebuild route cache method. This will rewrite the routes for our simple CMS each time a page is added or updated
         * @return void
         */
            private function __rebuildRouteCache() {
                $pages = $this->find('all');
                $filename = TMP . 'cache' . DS . 'routes.php';
                $buffer = "<?php \r\n";

                foreach($pages as $page) {
                    $buffer .= 'Router::connect("'. $page['Page']['slug'] .'", array( "controller" => "pages", "action" => "dynamic_display"));';
                    $buffer .= "\r\n";
                }

                $buffer .= "?>";

                file_put_contents($filename, $buffer);
            }



    }
?>

routes.php文件

<?php
#..snip
/**
 * Include our route cache if it exists
 */
    $fname = TMP . 'cache' . DS . 'routes.php';
    if(file_exists($fname)) {
        require_once $fname;
    }


/**
 * Load all plugin routes. See the CakePlugin documentation on
 * how to customize the loading of plugin routes.
 */
    CakePlugin::routes();

/**
 * Load the CakePHP default routes. Only remove this if you do not want to use
 * the built-in default routes.
 */
    require CAKE . 'Config' . DS . 'routes.php';
?>

这不是我想要的所有路线,但对我的情况来说这是一个可行的解决方案。希望这对其他人也有用。

2 个答案:

答案 0 :(得分:0)

当您编写require CAKE . 'Config' . DS . 'routes.php';时,您基本上包括已经是全部路由的cakephp默认路由。通过这一行配置的路由是:

Router::connect('/:controller', array('action' => 'index'));
Router::connect('/:controller/:action/*');

您在#... other routes中定义的所有路线仍然有效。

但是,如果您在应用程序中使用Cakephp默认路由,那么它们现在会被您自定义的“全能”路径所覆盖。

答案 1 :(得分:0)

在AppController.php中创建一个函数,如下所示。

public function appError($error) {
    $this->redirect('/catch-all-route',301,false);
}

上面的函数将捕获所有在routes.php中没有的错误页面

现在为/catch-all-route

创建一个规则
Router::connect('/catch-all-route', 
 array('controller' => 'pages', 'action' => 'dynamic_display', 'home'));

这样您就可以匹配所有未在routes.php中定义的网址