沃梦达 / 编程问答 / php问题 / 正文

在 Laravel 4 中找不到控制器类

Controller class not found in Laravel 4(在 Laravel 4 中找不到控制器类)

本文介绍了在 Laravel 4 中找不到控制器类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

尝试运行控制器时出现以下错误

I have the following error while trying to run my controller

找不到控制器类

我的 routes.php 文件中有这段代码

I have this code in my routes.php file

Route::get('cms/create-page', 'AdminCMSController@create_page');
Route::post('cms/createpage','AdminCMSController@createpage');
Route::controller('cms','AdminCMSController');

这是我控制器中的代码

class AdminCMSController extends BaseController {
    public function create_page() {
    }

    public function createpage() {
    }
}

我该如何解决?

推荐答案

如果您没有将 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 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 更新您的作曲家自动加载文件.

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

如果你使用 PSR-0,你需要像这样命名你的类:

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

<?php namespace Acmecontrollers;

class AdminCMSController extends BaseController {
(...)
}

Acme 参考感到好奇?我也是.参考维基百科.

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

这篇关于在 Laravel 4 中找不到控制器类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:在 Laravel 4 中找不到控制器类

基础教程推荐