Controller class not found in Laravel 4

浪子不回头ぞ 提交于 2019-12-17 21:21:14

问题


I have the following error while trying to run my controller

Controller class not found

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

And this is the code in my Controller

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

    public function createpage() {
    }
}

How can I fix it?


回答1:


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

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

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

  2. 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

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 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.

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',

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. Use PSR-0 for autoloading classes. Imagine that you have the following folder structure:

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

    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/"
        }
    },
    

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

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

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

<?php namespace Acme\controllers;

class AdminCMSController extends BaseController {
(...)
}

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



来源:https://stackoverflow.com/questions/25099054/controller-class-not-found-in-laravel-4

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!