Laravel 4 中未找到控制器类
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/25099054/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Controller class not found in Laravel 4
提问by Deepak Goyal
I have the following error while trying to run my controller
尝试运行控制器时出现以下错误
Controller class not found
找不到控制器类
I have this code in my routes.php
file
我的routes.php
文件中有此代码
Route::get('cms/create-page', 'AdminCMSController@create_page');
Route::post('cms/createpage','AdminCMSController@createpage');
Route::controller('cms','AdminCMSController');
And this is the code in my Controller
这是我的控制器中的代码
class AdminCMSController extends BaseController {
public function create_page() {
}
public function createpage() {
}
}
How can I fix it?
我该如何解决?
回答by Luís Cruz
If you didn't move the controllers
directory from the original location (which is ?project_root?/app/controllers/
, you must guarantee that:
如果您没有controllers
从原始位置移动目录(即?project_root?/app/controllers/
,您必须保证:
Laravel's autoload has the
controller
directory. Navigate to?project_root?/app/start/global.php
. You need to have something like this:(...) ClassLoader::addDirectories(array( app_path().'/commands', app_path().'/controllers', app_path().'/models', app_path().'/database/seeds', )); (...)
Take notice to this line
app_path().'/controllers'
. It must exist.Also, open your
composer.json
file and verify that the following lines exist:(...) "autoload": { "classmap": [ "app/commands", "app/controllers", "app/database/migrations", "app/database/seeds", "app/tests/TestCase.php" ], (...)
Make sure that you have the line with
app/controllers
Laravel 的自动加载有
controller
目录。导航到?project_root?/app/start/global.php
。你需要有这样的东西:(...) ClassLoader::addDirectories(array( app_path().'/commands', app_path().'/controllers', app_path().'/models', app_path().'/database/seeds', )); (...)
注意这条线
app_path().'/controllers'
。它必须存在。此外,打开您的
composer.json
文件并验证以下行是否存在:(...) "autoload": { "classmap": [ "app/commands", "app/controllers", "app/database/migrations", "app/database/seeds", "app/tests/TestCase.php" ], (...)
确保你有这条线
app/controllers
If you have this lines and you still get the same message, go to your project root and run the following command from the command-line composer dumpautoload -o
.
如果您有这些行并且仍然收到相同的消息,请转到您的项目根目录并从命令行运行以下命令composer dumpautoload -o
。
Laravel works with Composer, which is a dependency management tool for PHP. It also prepares an autoload file for all your project classes (see composer docs). When you run the composer dumpautoload
command, it will create some files within ?project_root?/vendor/composer
.
Laravel 与Composer一起工作,Composer是 PHP 的依赖管理工具。它还为您的所有项目类准备一个自动加载文件(请参阅 composer docs)。当您运行该composer dumpautoload
命令时,它会在?project_root?/vendor/composer
.
Make sure that you can find the class AdminCMSController
in the file ?project_root?/vendor/composer/autoload_classmap.php
. You should see something like this:
确保您可以AdminCMSController
在文件中找到该类?project_root?/vendor/composer/autoload_classmap.php
。您应该会看到如下内容:
'AdminCMSController' => $baseDir . '/app/controllers/AdminCMSController.php',
If you have changed the default location of your controllers
directory, you have to do either one of the following steps. However, since you are not defining a namespace in your class, it doesnt seem likely that this is your problem:
如果您更改了controllers
目录的默认位置,则必须执行以下任一步骤。但是,由于您没有在类中定义命名空间,因此这似乎不是您的问题:
Use PSR-0for autoloading classes. Imagine that you have the following folder structure:
/app /commands /config /database /Acme /controllers
You have to specify the
Acme
folder in yourcomposer.json
, like this:"autoload": { "classmap": [ "app/commands", "app/database/migrations", "app/database/seeds", "app/tests/TestCase.php" ], "psr-0": { "Acme": "app/" } },
After this you need to update you composer autoload files with the command
composer dumpautoload
.If you do not want to use the PSR-0 for autoloading, you need to change your routes file from this
Route::controller('cms','AdminCMSController');
to this:
Route::controller('cms','Acme\controllers\AdminCMSController');
使用PSR-0自动加载类。假设您有以下文件夹结构:
/app /commands /config /database /Acme /controllers
您必须
Acme
在您的 中指定文件夹composer.json
,如下所示:"autoload": { "classmap": [ "app/commands", "app/database/migrations", "app/database/seeds", "app/tests/TestCase.php" ], "psr-0": { "Acme": "app/" } },
在此之后,您需要使用命令更新您的 Composer 自动加载文件
composer dumpautoload
。如果您不想使用 PSR-0 进行自动加载,则需要从此更改您的路由文件
Route::controller('cms','AdminCMSController');
对此:
Route::controller('cms','Acme\controllers\AdminCMSController');
IF you use PSR-0, you need to namespace your classes like this:
如果你使用 PSR-0,你需要像这样为你的类命名:
<?php namespace Acme\controllers;
class AdminCMSController extends BaseController {
(...)
}
Curious about the Acme
reference? I was too. Refer to the wikipedia.
对Acme
参考感到好奇?我也是。请参阅维基百科。