doushi2845 2014-12-01 20:21
浏览 95
已采纳

Laravel命名空间与目录

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?

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

  • 写回答

1条回答 默认 最新

  • dongzou1964 2014-12-01 20:40
    关注

    To be honest, I've never heard of any PSR Standards for naming a directory in Laravel. 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.

    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.

    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!

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 TLS1.2协议通信解密
  • ¥40 图书信息管理系统程序编写
  • ¥20 Qcustomplot缩小曲线形状问题
  • ¥15 企业资源规划ERP沙盘模拟
  • ¥15 树莓派控制机械臂传输命令报错,显示摄像头不存在
  • ¥15 前端echarts坐标轴问题
  • ¥15 ad5933的I2C
  • ¥15 请问RTX4060的笔记本电脑可以训练yolov5模型吗?
  • ¥15 数学建模求思路及代码
  • ¥50 silvaco GaN HEMT有栅极场板的击穿电压仿真问题