Cakephp 2.4.5 默认控制器未加载

Cakephp 2.4.5 Default Controller not loading

我是 cakephp 的新手,正在尝试将默认控制器加载为 Pages

这是我的路线:

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

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

当我 运行 "http://localhost/project/index.php" then its working fine but try with "http://localhost/project/" 它没有加载默认控制器(页面)

没有 htaccess 和有 htaccess 它给出同样的问题。

这是错误:

Controller class ProjectController could not be found.

错误:

The requested address '/project/index.php/project/' was not found on this server.

您在 运行 子目录中,因此您应该设置 RewriteBase:

<IfModule mod_rewrite.c> 
    RewriteEngine On 
    RewriteBase /project/
    RewriteCond %{REQUEST_FILENAME} !-d 
    RewriteCond %{REQUEST_FILENAME} !-f 
    RewriteRule ^(.*)$ /project/index.php?url= [QSA,L] 
</IfModule

根据 cakephp 手册:

http://book.cakephp.org/3.0/en/development/routing.html#redirect-routing

你应该试试

Router::scope('/', function ($routes) {
$routes->redirect(
    '/home/*',
    ['controller' => 'Articles', 'action' => 'view'],
    ['persist' => true]
    // Or ['persist'=>['id']] for default routing where the
    // view action expects $id as an argument.
);
})

而不是 Router::redirect。您应该尝试一次此方法,可能问题已得到解决。

已解决

在应用程序控制器中添加了 baseUrl:

function beforeRender(){
  $this->set('baseUrl', 'http://'.$_SERVER['SERVER_NAME'].Router::url('/'));
}

从 Core.php 中删除了 App.baseUrl :

Configure::write('App.baseUrl', env('SCRIPT_NAME'));

现在可以正常工作了: http://localhost/app/