为什么我的Laravel模块中出现“未找到类”错误?

时间:2023-01-20 16:45:05

I am using "laravel/framework": "4.2. *" version, and I want to use the module system for my project. I have followed the instructions provided in this document.

我正在使用“laravel / framework”:“4.2。*”版本,我想将模块系统用于我的项目。我已按照本文档中提供的说明进行操作。

I can create modules by using the command: php artisan modules:create module_name. I have created an admin module in my app directory, and the module's directory structure has been created.

我可以使用以下命令创建模块:php artisan modules:create module_name。我在app目录中创建了一个管理模块,并且已经创建了模块的目录结构。

I am using DB::select('some SQL statement') in one of the actions of the controller from the admin module, but it is giving me the following error:

我在管理模块的控制器的一个操作中使用DB :: select('some SQL statement'),但是它给了我以下错误:

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

找不到“App \ Modules \ Admin \ Controllers \ DB”类。

Why is it not able to find this class?

为什么找不到这门课呢?

2 个解决方案

#1


13  

When using DB or any other Laravel facades outside the root namespace, you need to make sure you actually use the class in the root namespace. You can put a \ before the class.

在根命名空间之外使用DB或任何其他Laravel外观时,您需要确保在根命名空间中实际使用该类。你可以在课前放一个\。

\DB::select(...)

Or you can use the use keyword in your class file to allow the use of a different namespaced class without explicitly writing out the namespace each time you use it.

或者,您可以在类文件中使用use关键字,以允许使用不同的命名空间类,而无需在每次使用时明确写出命名空间。

<?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 use keyword always assumes it is loading a namespace from the root namespace. Therefore a fully qualified namespace is always required with use.

请注意,use关键字始终假定它从根命名空间加载命名空间。因此,始终需要使用完全限定的命名空间。

#2


0  

or else you can use autoload in composer.json

或者你可以在composer.json中使用自动加载

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

#1


13  

When using DB or any other Laravel facades outside the root namespace, you need to make sure you actually use the class in the root namespace. You can put a \ before the class.

在根命名空间之外使用DB或任何其他Laravel外观时,您需要确保在根命名空间中实际使用该类。你可以在课前放一个\。

\DB::select(...)

Or you can use the use keyword in your class file to allow the use of a different namespaced class without explicitly writing out the namespace each time you use it.

或者,您可以在类文件中使用use关键字,以允许使用不同的命名空间类,而无需在每次使用时明确写出命名空间。

<?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 use keyword always assumes it is loading a namespace from the root namespace. Therefore a fully qualified namespace is always required with use.

请注意,use关键字始终假定它从根命名空间加载命名空间。因此,始终需要使用完全限定的命名空间。

#2


0  

or else you can use autoload in composer.json

或者你可以在composer.json中使用自动加载

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