【问题标题】:Cakephp route - Missing ControllerCakephp 路由 - 缺少控制器
【发布时间】:2014-02-06 00:40:08
【问题描述】:

我正在尝试在我的 CakePHP 应用程序中创建一个静态的 /terms 页面。我在app/View/Pages 中有一个terms.ctp,在app/Config/routes.php 中有以下内容

<?php
/**
 * Routes configuration
 *
 * In this file, you set up routes to your controllers and their actions.
 * Routes are very important mechanism that allows you to freely connect
 * different URLs to chosen controllers and their actions (functions).
 *
 * CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
 * Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
 *
 * Licensed under The MIT License
 * For full copyright and license information, please see the LICENSE.txt
 * Redistributions of files must retain the above copyright notice.
 *
 * @copyright     Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
 * @link          http://cakephp.org CakePHP(tm) Project
 * @package       app.Config
 * @since         CakePHP(tm) v 0.2.9
 * @license       http://www.opensource.org/licenses/mit-license.php MIT License
 */
/**
 * Here, we are connecting '/' (base path) to controller called 'Pages',
 * its action called 'display', and we pass a param to select the view file
 * to use (in this case, /app/View/Pages/home.ctp)...
 */
    Router::connect('/', array('controller' => 'pages', 'action' => 'display', 'home'));
/**
 * ...and connect the rest of 'Pages' controller's URLs.
 */
    Router::connect('/pages/*', array('controller' => 'pages', 'action' => 'display'));

/**
 * 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';

    Router::connect('/terms', array('controller' => 'pages', 'action' => 'display', 'terms'));

但是当访问http://cakeapp.com/terms 时,我得到一个缺少控制器的错误:

Error: TermsController could not be found.

有什么想法吗?

【问题讨论】:

  • http://cakeapp.com/terms/ 有效吗?
  • 不,我收到一个神秘的缺少控制器错误

标签: php url cakephp routing


【解决方案1】:

将路由向上移动:(在调用CakePlugin::routes();之前),否则,该路由将被忽略

Router::connect('/', array('controller' => 'pages', 'action' => 'display', 'home'));
/**
* ...and connect the rest of 'Pages' controller's URLs.
*/
Router::connect('/pages/*', array('controller' => 'pages', 'action' => 'display'));

Router::connect('/terms/', array('controller' => 'pages', 'action' => 'display', 'terms'));

【讨论】:

  • 是的,自定义路由必须先于require CAKE . 'Config' . DS . 'routes.php';,其中放置了全能路由/:controller/:action/*
  • 掌心/脸...刚刚度过了愉快的时光。这通过省略尾随的“/”来工作。谢谢!
【解决方案2】:

尝试删除结尾的斜线:

Router::connect(
    '/terms', 
    array(
        'controller' => 'pages', 
        'action' => 'display', 'terms'
    )
);

【讨论】:

  • 仍然缺少控制器,只是添加了其余的 routes.php,我不知道发生了什么
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2018-06-07
  • 1970-01-01
  • 2013-12-16
  • 2014-12-15
  • 1970-01-01
  • 2015-06-29
  • 1970-01-01
相关资源
最近更新 更多