Laravel 5 命名空间

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

Laravel 5 namespaces

laravellaravel-5

提问by Maeh

I just downloaded Laravel 5 and started migrating to it. However, I find the required use of namespaces reallyannoying.

我刚刚下载了 Laravel 5 并开始迁移到它。但是,我发现必须使用命名空间真的很烦人。

I don't feel like I am getting much from it, other than cluttering my code.

除了使我的代码变得混乱之外,我不觉得我从中得到了多少。

How can I disable the namespacing requirement?

如何禁用命名空间要求?

回答by lukasgeiter

I don't think you should disable or remove namespaces. The main reason for namespacing is to avoid conflicts with classes that have the same name. As soon as an application gets larger you will have classes that have the same name. Example from the Framework source:

我认为您不应该禁用或删除命名空间。命名空间的主要原因是为了避免与具有相同名称的类发生冲突。一旦应用程序变大,您就会拥有同名的类。来自框架源的示例:

Illuminate\Console\Applicationand Illuminate\Foundation\Application

Illuminate\Console\ApplicationIlluminate\Foundation\Application

Both are called the same. Only because of the namespacing you can import the right class. Of course you could also name them:

两者称为相同。只有因为命名空间,您才能导入正确的类。当然,您也可以命名它们:

ConsoleApplicationand FoundationApplication

ConsoleApplicationFoundationApplication

But while the namespace normally is only used when importing a class at the top of a file:

但是,虽然命名空间通常仅在导入文件顶部的类时使用:

use `Illuminate\Console\Application`

The name itself is used everywhere in the code. That's something that really clutters up your code, too long class names.

名称本身在代码中随处可见。这确实会使您的代码变得混乱,类名太长。

Besides the naming thing, namespaces also encourage better structure and help with knowing where your files are. That's because Laravel's default structure is PSR-4compliant. That means if you have a controller App\Http\Controllers\HomeControlleryou can be certain that you will find a HomeController.phpunder app/Http/Controllers.

除了命名之外,命名空间还鼓励更好的结构并有助于了解文件的位置。那是因为 Laravel 的默认结构是PSR-4兼容的。这意味着如果你有一个控制器,App\Http\Controllers\HomeController你可以肯定你会找到一个HomeController.phpunder app/Http/Controllers

I am aware of that, but it's not needed in the project I am working on.

我知道这一点,但在我正在从事的项目中不需要它。

Maybe it doesn't make sense for the current project but getting used to namespaces will help you tackle bigger projects in the future

也许它对当前项目没有意义,但习惯命名空间将帮助你在未来处理更大的项目

And being a Sublime Text user, which doesn't have autoimport, it really gets to be a pain

作为一个没有自动导入的 Sublime Text 用户,这真的很痛苦

I don't know Sublime Text that well, but CodeIntelmight have auto import. Otherwise consider switching to another editor / IDE. I can highly recommend JetBrains PhpStorm

我不太了解 Sublime Text,但CodeIntel可能有自动导入功能。否则考虑切换到另一个编辑器/IDE。我强烈推荐 JetBrains PhpStorm



In the end, if you still don't want to use namespaces, keep using Laravel 4 or search for another framework that follows less good practices...

最后,如果您仍然不想使用命名空间,请继续使用 Laravel 4 或搜索另一个遵循较少良好实践的框架...



Removing namespaces from your app classes

从您的应用程序类中删除命名空间

While a totally don't recommend this, it is possible to at least remove some of the namespacing in your application.

虽然完全不推荐这样做,但至少可以删除应用程序中的一些命名空间。

For example the default controller namespace App\Http\Controllerscan be changed to no namespace at all in RouteServiceProvider:

例如,App\Http\Controllers可以将默认控制器命名空间更改为根本没有命名空间RouteServiceProvider

protected $namespace = '';

And for your models you can just remove the namespace in the file and your good. But keep in mind that without namespaces PSR-4 autoloading won't work anymore. You will have to autoload your files using classmapin composer.json

对于您的模型,您只需删除文件中的命名空间即可。但请记住,如果没有命名空间,PSR-4 自动加载将不再起作用。您必须使用classmapin自动加载文件composer.json

回答by ddn

You can avoid using namespaces for own classes by defining them in the global namespace in your composer.json file. Like this:

您可以通过在 composer.json 文件的全局命名空间中定义它们来避免为自己的类使用命名空间。像这样:

"autoload": {
    "psr-0": {
    "": ["app/Http/Controllers/",
        "app/models/",
        "app/helpers"
        ]
},

You will also have to change your app/Providers/RouteServiceProvider.php to:

您还必须将您的 app/Providers/RouteServiceProvider.php 更改为:

protected $namespace = '';

for routing to work.

用于路由上班。