简体   繁体   English

Laravel使用目录命名空间

[英]Laravel namespacing with directories

So I'm developing quite a large application which has a large number of controllers. 因此,我正在开发具有大量控制器的大型应用程序。 I'm wondering what the proper PSR compatible practice would be for this situation? 我想知道这种情况下正确的PSR兼容做法是什么?

A directory example: 目录示例:

project\workbench\stevebauman\package\src\controllers\WorkOrder\WorkOrderController.php

An example use statement: 使用语句示例:

use Stevebauman\Package\Controllers\WorkOrder\WorkOrderController;

If I sub-namespace the work order folder (since there are many other controllers to do with work orders), should I namespace the Controller with WorkOrder ? 如果我对工作订单文件夹进行子命名(因为还有许多其他控制器要处理工作订单),那么我应该使用WorkOrderController命名吗? Or since this is already in a sub-namespace, should I just be using: 或因为这已经在子命名空间中,所以我应该只使用:

use Stevebauman\Package\Controllers\WorkOrder\Controller;

For the main work order controller, and do away with the prefix entirely? 对于主工作订单控制器,完全取消前缀吗?

I'm looking for a PSR standard if it exists. 我正在寻找PSR标准(如果存在)。 I'm not sure what is generally used in large directory projects. 我不确定大型目录项目中通常使用什么。 For example what if work orders had attachments? 例如,如果工作单带有附件怎么办? Would I do something like this (sub-namespaces indicate new sub-folder)? 我会做这样的事情(子命名空间表示新的子文件夹)吗?

use Stevebauman\Package\Controllers\WorkOrder\Attachment\Controller;

or: 要么:

use Stevebauman\Package\Controllers\WorkOrder\AttachmentController;

or even: 甚至:

use Stevebauman\Package\Controllers\WorkOrderAttachmentController;

I hope my confusion here is justified. 我希望我的困惑在这里是合理的。 Can anyone lend some suggestions? 谁能提出一些建议? Thanks! 谢谢!

To be honest, I've never heard of any PSR Standards for naming a directory in Laravel. 老实说,我从未听说过在Laravel中命名目录的任何PSR标准。 I don't think there is really a set "rule" when naming/arranging your controllers. 我认为在命名/安排控制器时并没有真正的“规则”。 There are some who name their controllers based on actions: 有些人根据动作命名其控制器:

app/controllers/CreateController.php
app/controllers/DeleteController.php

And in these controllers are all the different functions like create($object) or delete($object) which do different things based on the object being passed to it. 在这些控制器中,所有不同的函数(例如create($object)delete($object)根据传递给它的对象执行不同的操作。

Another way is one controller per object: 另一种方法是每个对象一个控制器:

app/controllers/PersonController.php
app/controllers/DogController.php

And in these controllers are the CRUD functions for that particular object. 在这些控制器中,是该特定对象的CRUD功能。

Lastly, there are ways to organize these controller based on the models they are based off of (as provided in your question): 最后,有几种方法可以根据它们所基于的模型来组织这些控制器(如您的问题所提供):

app/controllers/Person/PersonController.php
app/controllers/Person/SiblingController.php
etc.

However you choose to do it is completely up to your personal preferences, but the one thing to keep in mind is BE CONSISTENT , and I can't stress that enough. 然而,你选择做完全取决于你的个人喜好,但有一点要记住的是一致的,我不能强调不够。 This is likely going to be an opinionated question, but the way I see it is; 这可能是一个自以为是的问题,但是我的看法是这样。 whatever works best for your development tastes is what you should use, unless instructed otherwise. 除非另有说明,否则应该使用最适合自己开发口味的东西。

Hope that helps! 希望有帮助!

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

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