Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
255 views
Welcome To Ask or Share your Answers For Others

1 Answer

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

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

<?php namespace Acmecontrollers;

class AdminCMSController extends BaseController {
(...)
}

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...