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