Laravel 模型目录和命名空间

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/28463819/
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:54:22  来源:igfitidea点击:

Laravel model directory and namespace

phplaravelnamespaceslaravel-5

提问by imperium2335

I am starting out with Laravel 5 and as first order of business I want to move all my models into a folder called Models.

我从 Laravel 5 开始,作为第一项业务,我想将所有模型移动到一个名为 Models 的文件夹中。

But how can I access those without specifying the namespace like in the following?:

但是如何在不指定命名空间的情况下访问它们,如下所示?:

...

class UserRolesTableSeeder extends Seeder {

    public function run()
    {
        DB::table('user_roles')->delete();

        App\Models\UserRoles::create(['name' => 'CREATE_USER']);
    }

}

回答by bandrei2408

Go into your composer.json and add at the end of "autoload": "classmap" this line "app/models". This way you are telling laravel to autoload those clases. After that, run a composer update and it should work.

进入你的 composer.json 并在“autoload”的末尾添加:“classmap”这一行“app/models”。这样你就告诉 laravel 自动加载这些类。之后,运行 Composer 更新,它应该可以工作。

You can also create a service provider to access models without namespaces.

您还可以创建一个服务提供者来访问没有命名空间的模型。

To create a service provider, here is what you have to do :

要创建服务提供者,您必须执行以下操作:

1) Create a file in your models directory and name it ModelsServiceProvider.php

1) 在你的模型目录中创建一个文件并将其命名为 ModelsServiceProvider.php

2) Inside of it write this code

2)在里面写这个代码

<?php
namespace App\Models;

use Illuminate\Support\ServiceProvider;

class ModelsServiceProvider extends ServiceProvider {

public function register()
{

    $this->app->booting(function()
    {
        $loader = \Illuminate\Foundation\AliasLoader::getInstance();
        $loader->alias('UserRoles', 'App\Models\UserRoles');

    });

}

3) Go into app/config/app.php and under providers array add this line 'App\Models\ModelsServiceProvider'

3) 进入 app/config/app.php 并在 providers 数组下添加这一行 'App\Models\ModelsServiceProvider'

You can also add directly your aliases for classes under the aliases array inside app/config/app.php.

您还可以在 app/config/app.php 中的 aliases 数组下直接添加类的别名。

回答by Sabrina Leggett

Alternatively, you can just load your models into the global namespace like you were doing before! It's a bit scary to go against the docs but so far we haven't had any issues with it.

或者,您可以像以前一样将模型加载到全局命名空间中!违背文档有点可怕,但到目前为止我们还没有遇到任何问题。

To make sure your models are still loaded, you just need to add the reference to your composer.json:

为了确保你的模型仍然被加载,你只需要添加对你的 composer.json 的引用:

(assumes your namespace is App\Models)

(假设您的命名空间是 App\Models)

"autoload": {

    "classmap": [
        ...
        "app/Models/"
    ],
    ...
    "": [
        "app/Models/"
    ]

be sure to run composer dump-autoload

一定要跑 composer dump-autoload