Controller class not found in Laravel 4

后端 未结 1 1222
予麋鹿
予麋鹿 2020-12-12 06:22

I have the following error while trying to run my controller

Controller class not found

I have this code in my routes.php

相关标签:
1条回答
  • 2020-12-12 07:08

    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.

    0 讨论(0)
提交回复
热议问题