简体   繁体   English

如何在Laravel 5中使用PSR4命名空间?

[英]How does name spacing works using PSR4 in laravel 5?

I am a novice laravel developer and I am trying to understand how namespacing works in here. 我是laravel新手开发人员,在这里我试图了解命名空间的工作原理。 So I was learning about the Repository pattern and decided to go ahead and implement it in my project. 因此,我正在学习存储库模式,并决定继续在我的项目中实施它。

So I created a directory called archive and then loaded it using PSR4 in my composer.json file like so: 所以我创建了一个名为archive的目录,然后使用PSR4将其加载到composer.json文件中,如下所示:

"Archive\\": "archive/"

Now I created a Repositories folder in my archive folder where I will be creating all the repositories. 现在,我在archive文件夹中创建了一个Repositories文件夹,将在其中创建所有存储库。 And I am namespacing files in it like so: 我在其中给文件命名,如下所示:

namespace Archive\Repositories;

Which seems to working fine. 这似乎工作正常。 Then I created a Contracts folder inside the Repositories folder which will hold all the interfaces to the implementations I am going to use like UserRepositoryInterface for example. 然后,我在Repositories文件夹内创建了一个Contracts文件夹,该文件夹将保存我将要使用的实现的所有接口,例如UserRepositoryInterface And I am namespacing files in the Contracts folder like so: 我正在命名合同文件夹中的文件,如下所示:

namespace Archive\Repositories\Contracts;

Which is working fine too. 这也很好。

Now my doubt lies in the concrete implementations I am trying to make in the Repositories folder. 现在,我的疑问在于我要在“ Repositories文件夹中进行的具体实现。 Like for example there is a DbUserRepository which implements The UserRepositoryInterface in the Contracts folder. 例如像有一个DbUserRepository它实现了UserRepositoryInterface在合同文件夹。

Now since I am new to this I tried: 现在,由于我是新手,所以尝试了:

class DbUserRepository implements Contacts\UserRepositoryInterface

And it works just fine but then I thought I should use it on top of the file like so: 它工作正常,但后来我想应该在文件顶部use它,如下所示:

use Contacts\UserRepositoryInterface;

And I could just do: 我可以做:

class DbUserRepository implements UserRepositoryInterface

And it should work fine according to me but it gives me a class not found exception but when I do something like: 它对我来说应该可以正常工作,但它给了我一个类未发现异常,但是当我做类似的事情时:

use Archive\Repositories\Contacts\UserRepositoryInterface;

It works fine now. 现在工作正常。 But this is where I am blurry. 但是,这是我很模糊的地方。 Inside the DbUserRepository I am in the nampspace of Archive\\Repositories already so why doesn't it just go on to look into the Contracts folder from there? DbUserRepository内部,我已经在Archive\\Repositories的nampspace中,为什么不继续从那里查看Contracts文件夹呢? Why do I need to specify the full this as use Archive\\Repositories\\Contacts\\UserRepositoryInterface; 为什么我需要use Archive\\Repositories\\Contacts\\UserRepositoryInterface;指定完整的use Archive\\Repositories\\Contacts\\UserRepositoryInterface;

Why can't I just say: 我为什么不能说:

use Contacts\UserRepositoryInterface;

I hope my question is not too confusing. 我希望我的问题不要太混乱。 Although my code is working now but I am blurry how namespacing works. 尽管我的代码现在可以正常工作,但是我不清楚命名空间的工作原理。

The rules are pretty simple: 规则很简单:

  1. All namespace and use statements always use fully qualified names (FQN), meaning they always start from the global namespace and are not relative to anything else. 所有namespaceuse语句始终使用完全限定名称 (FQN),这意味着它们始终从全局名称空间开始,并且与其他任何名称都不相关。 use Foo\\Bar always means \\Foo\\Bar , no matter what namespace you're in. use Foo\\Bar始终表示\\Foo\\Bar ,无论您使用的是什么名称空间。

  2. All literal mentions of names inside the rest of the code are resolved relative to the current namespace and/or aliases established with use statements. 其余代码中对名称的所有文字提及均相对于use语句建立的当前名称空间和/或别名解决。 new Foo , extends Foo and such either mean __NAMESPACE__\\Foo , or whatever Foo you might have aliased in some use statement. new Fooextends Foo ,这意味着__NAMESPACE__\\Foo ,或者您可能在某些use语句中use别名的任何Foo

If you want to shorten names, you need to use use statements which use the FQN of the class, not relative to the current namespace. 如果要缩短名称,则需要使用use语句,该语句使用类的FQN,而不是相对于当前名称空间的FQN。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM