简体   繁体   English

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

[英]Why am I getting a “class not found” error in my Laravel module?

I am using "laravel/framework": "4.2. *" version, and I want to use the module system for my project. 我正在使用“laravel / framework”:“4.2。*”版本,我想将模块系统用于我的项目。 I have followed the instructions provided in this document . 我已按照本文档中提供的说明进行操作。

I can create modules by using the command: php artisan modules:create module_name . 我可以使用以下命令创建模块: 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. 我在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? 为什么找不到这门课呢?

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. 在根命名空间之外使用DB或任何其他Laravel外观时,您需要确保在根命名空间中实际使用该类。 You can put a \\ before the class. 你可以在课前放一个\\

\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. 请注意, use关键字始终假定它从根命名空间加载命名空间。 Therefore a fully qualified namespace is always required with use . 因此,始终需要use完全限定的命名空间。

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

使用Illuminate \\ Support \\ Facades \\ DB;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM