找不到 Laravel 5 模型命名空间类

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/28488419/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 10:55:22  来源:igfitidea点击:

Laravel 5 Model namespace class not found

phplaravelnamespaceslaravel-5

提问by imperium2335

I am trying to store my models in a custom namespace and directory structure as shown here:

我正在尝试将我的模型存储在自定义命名空间和目录结构中,如下所示:

enter image description here

在此处输入图片说明

I have:

我有:

namespace Modules\Core;

use App\Http\Controllers\Controller;

class TestController extends Controller {

    public function index(){
        $user = new User;
        return view('core::test');
    }

}

But I am getting:

但我得到:

FatalErrorException in TestController.php line 8:
Class 'Modules\Core\User' not found

Which is wrong anyway, so I thought it must be 'Modules\Core\Models\User'. I tried this and I still got the same error (just with a different class name).

无论如何这是错误的,所以我认为它必须是'Modules\Core\Models\User'。我试过了,但仍然出现相同的错误(只是使用了不同的类名)。

My model:

我的型号:

namespace Modules\Core;

use Illuminate\Database\Eloquent\Model as Eloquent;

class User Extends Eloquent {
    protected $table = 'users';
}

How can I get access to this model in the TestController?

如何在 TestController 中访问此模型?



Route::group(array('namespace' => 'Modules\Core\Controllers'), function() {
    Route::get('/test', ['uses' => 'TestController@index']);
});

采纳答案by Joel Hinz

If your controller is stored in Modules/Core/Controllers, the namespace should be namespace Modules\Core\Controllers;

如果您的控制器存储在 Modules/Core/Controllers 中,则命名空间应为 namespace Modules\Core\Controllers;

And likewise, if the model is stored in Modules/Core/Models, its namespace is namespace Modules\Core\Models;

同样,如果模型存储在 Modules/Core/Models 中,则其命名空间为 namespace Modules\Core\Models;

Then, in the controller, import it before using it:

然后,在控制器中,在使用之前导入它:

<?php namespace Modules\Core\Controllers;

use Modules\Core\Models\User;
use App\Http\Controllers\Controller;

class TestController extends Controller {

    public function index(){
        $user = new User;
        return view('core::test');
    }

}

回答by Skywalker

I had the same problem as above. In my case I had the following namespace:

我遇到了与上述相同的问题。就我而言,我有以下命名空间:

namespace Modules\XMLApi;

I got the same error as above. When I changed the namespace to the following:

我得到了与上面相同的错误。当我将命名空间更改为以下内容时:

namespace Modules\XmlApi;

Then run the following command: composer dump-autoload

然后运行以下命令:composer dump-autoload

Then it worked.

然后它起作用了。

回答by Marcin Nabia?ek

You should edit your routes.phpfile:

你应该编辑你的routes.php文件:

Route::group(array('namespace' => 'Modules\Core\Controllers'), function() {
    Route::get('/test', ['uses' => '\Modules\Core\TestController@index']);
});

to use full together with namespace

与命名空间一起使用 full