Why am I getting "class not found" error in my Laravel module?

I am using "laravel / framework" version: "4.2. *" And I want to use a modular system for my project. I followed the instructions in this document .

I can create modules using the command: php artisan modules:create module_name

. I created an admin module in my application directory and the module directory structure was created.

I am using DB::select('some SQL statement')

in one of the actions the controller from the admin module, but it gives me the following error:

Class 'App \ Modules \ Admin \ Controllers \ DB' not found.

Why can't he find this class?

+3


source to share


3 answers


When using DB

or any other Laravel facades outside of the root namespace, you need to make sure that you are actually using the class in the root namespace. You can put \

in front of the class.

\DB::select(...)

      

Or, you can use a keyword use

in your class file to allow a different class with a namespace to be used without explicitly writing the namespace every time you use it.



<?php namespace App\Modules\Admin\Controllers;

use DB;
use BaseController;

class ModuleController extends BaseController {

    public function index()
    {
        // This will now use the correct facade
        $data = DB::select(...);
    }
}

      

Note that the keyword use

always assumes loading the namespace from the root namespace. Therefore use

, a full namespace is always required.

+13


source


otherwise you can use autoload in composer.json



"autoload": {
    "classmap": [
        "app/commands",
        "app/controllers",
        "app/models",
        "app/database/migrations",
        "app/database/seeds",
        "app/tests/TestCase.php"
    ]
},

      

0


source


use Lighting \ Support \ Facades \ DB;

0


source







All Articles