Laravel 5名称空间 [英] Laravel 5 namespaces

查看:90
本文介绍了Laravel 5名称空间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚刚下载了Laravel 5并开始迁移到它.但是,我发现确实需要使用名称空间 really .

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

除了使我的代码混乱之外,我并没有从中得到很多.

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

如何禁用命名空间要求?

How can I disable the namespacing requirement?

推荐答案

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

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\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:

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.

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

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-4 compliant. That means if you have a controller App\Http\Controllers\HomeController you can be certain that you will find a HomeController.php under 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

作为Sublime Text用户,它没有自动导入功能,这确实让我很痛苦

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

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

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

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

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...


虽然完全不建议这样做,但至少可以删除应用程序中的某些命名空间.

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

例如,默认控制器名称空间App\Http\Controllers可以在RouteServiceProvider中更改为完全没有名称空间:

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

protected $namespace = '';

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

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 classmap in composer.json

这篇关于Laravel 5名称空间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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