为什么我得到一个“找不到类"?我的 Laravel 模块中的错误? [英] Why am I getting a "class not found" error in my Laravel module?

查看:23
本文介绍了为什么我得到一个“找不到类"?我的 Laravel 模块中的错误?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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.

我可以使用以下命令创建模块:php artisan modules:create module_name.我在我的app目录下创建了一个admin模块,模块的目录结构已经创建好了.

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.

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

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:

找不到类AppModulesAdminControllersDB".

Class 'AppModulesAdminControllersDB' not found.

为什么找不到这个类?

推荐答案

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

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::select(...)

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

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.

<?php namespace AppModulesAdminControllers;

use DB;
use BaseController;

class ModuleController extends BaseController {

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

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

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.

这篇关于为什么我得到一个“找不到类"?我的 Laravel 模块中的错误?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆