且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

在Laravel 4中找不到控制器类

更新时间:2022-04-21 18:38:26

如果您没有将controllers目录从原始位置(为«project_root»/app/controllers/移出),则必须确保:

If you didn't move the controllers directory from the original location (which is «project_root»/app/controllers/, you must guarantee that:

  1. Laravel的自动加载具有controller目录.导航至«project_root»/app/start/global.php.您需要具有以下内容:

  1. 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',
));
(...)

请注意此行app_path().'/controllers'.它必须存在.

Take notice to this line app_path().'/controllers'. It must exist.

此外,打开您的composer.json文件并验证以下行是否存在:

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"
    ],
(...)

确保您的行与app/controllers

如果您有这行内容,但仍然收到相同的消息,请转到项目根目录,然后从命令行composer dumpautoload -o运行以下命令.

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.

Laravel与 Composer 一起使用,后者是PHP的依赖项管理工具.它还会为您的所有项目类准备一个自动加载文件(请参阅composer文档).当您运行composer dumpautoload命令时,它将在«project_root»/vendor/composer中创建一些文件.

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.

确保可以在文件«project_root»/vendor/composer/autoload_classmap.php中找到类AdminCMSController.您应该会看到类似这样的内容:

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' => $baseDir . '/app/controllers/AdminCMSController.php',


如果更改了controllers目录的默认位置,则必须执行以下任一步骤.但是,由于您没有在类中定义名称空间,因此看来这不是您的问题:


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:

  1. 使用 PSR-0 进行自动加载类.假设您具有以下文件夹结构:

  1. Use PSR-0 for autoloading classes. Imagine that you have the following folder structure:

/app
    /commands
    /config
    /database
    /Acme
        /controllers

您必须在composer.json中指定Acme文件夹,如下所示:

You have to specify the Acme folder in your composer.json, like this:

"autoload": {
    "classmap": [
        "app/commands",
        "app/database/migrations",
        "app/database/seeds",
        "app/tests/TestCase.php"
    ],
    "psr-0": {
        "Acme": "app/"
    }
},

此后,您需要使用命令composer dumpautoload更新composer自动加载文件.

After this you need to update you composer autoload files with the command composer dumpautoload.

如果您不想使用PSR-0自动加载,则需要从此更改路由文件

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');

对此:

Route::controller('cms','Acme\controllers\AdminCMSController');

如果您使用PSR-0,则需要像这样对类命名空间:

IF you use PSR-0, you need to namespace your classes like this:

<?php namespace Acme\controllers;

class AdminCMSController extends BaseController {
(...)
}

Acme参考感兴趣吗?我也是. 请参阅***.

Curious about the Acme reference? I was too. Refer to the wikipedia.