[Proposal] Change Model location #1550
Comments
Seems to be a duplicate of #1091 |
I’m sure @taylorotwell has said a few times now that Laravel will not be shipped with a Models directory. You‘re free to organise your domain models how you see fit. Personally, I group them in directories relating to “domain”, i.e.
…and so on. |
if you just want to put it in the 'Models' folder, override make:model command. php artisan make:command ModelMakeCommand <?php
namespace App\Console\Commands;
use Illuminate\Foundation\Console\ModelMakeCommand as Command;
class ModelMakeCommand extends Command
{
/**
* Get the default namespace for the class.
*
* @param string $rootNamespace
* @return string
*/
protected function getDefaultNamespace($rootNamespace)
{
return "$rootNamespace\Models";
}
} |
This seems a bit excessive, just prefix your model name
The above will create |
And that would end up being pratically the same as to what I'm proposing, but coming out of the box would help the developers that are starting laravel to organize better their stuff. |
Whilst frustrating those who don’t store their models in a Models directory. |
I tottaly understand the reasonings here but this part still sounds funny. Models not in models. Maybe keep in it simple could be the best on these cases. |
I would imagine that a vast amount of those that do not use the models directory, do so because it has been the default for so long. I'd even go as far as to say that if the models directory was added by default, a lot of them would just use it that way. It's rare that I find someone not using it to be honest with you. That being said, it doesn't need to be default. The location of the models have no effect on how the codebase works. Realistically, people should be organising their code, so it may be a sensible thing to set as the default to help encourage that. |
+1 I can't collapse the models into a folder like I can do for controllers, providers, etc. Please consider moving all models to App/Models, plenty of people (myself included) are suffering through this. See this workaround, which is rather clunky: |
@connecteev that work around is unnecessary. If you're using an IDE (which you should be) just do a refactor > move class. Also, there's only a single reference to that model that means anything, and that's in the auth config. Super simple manual changes. You don't need to override the command, just prefix the model name with |
@connecteev There’s nothing stopping you putting your models in folders yourself. |
@ollieread thanks clarifying. I'm using Visual Studio code and don't see an option to refactor and move or refactor and rename. |
@connecteev Visual Studio Code isn’t a great editor for PHP out of the box. You’ll need to install an extension to do automatically re-factoring. |
What about those of us that keep our models in an entirely separate package?! The framework already provides the tools to allow you to put your models wherever you want them. |
If controllers are in the Controllers folder, Views are in the Views folder, routes are in the Routes folder, Commands are in the Commands folder, providers are in the Providers folder, Listeners, Mail, Jobs, Migrations, Configs, Middlewares, Requests, etc. Why aren't models in the Models folder? We know that some people don't put models in the Models folder. But why? A good explanation would be nice. |
It's kinda explained here https://laravel.com/docs/6.x/structure#introduction |
It just says "some developers". It clearly shows that, those "some developers", who break the conventions, are being favored in contrast to the MVC structure being placed in their respective directories based on practices used by many, who up to now followed the MVC practices from the start. I believe those "some developers" are confusing Models with Services https://softwareengineering.stackexchange.com/questions/230307/mvc-what-is-the-difference-between-a-model-and-a-service/230312. Laravel is favoring those "some developers" instead of enforcing a real and elegant MVC structure through folder structures and namespaces. I can have business logic, Traits, Contracts, VOs, Enums, Vendors, placed inside the Services directory instead of mixing them in the App directory along with models. I can use those Services wherever I want, either calling them from controllers, migrations, seeders or other services for that matter. I can even place a single file called helpers.php under the Services directory, which is way more elegant to look at in my IDE's sidebar. |
People are free to put their models in a “Models” folder if they wish:
|
That's understandable. However, wouldn't it be better to have it the other way around? Have the models placed in the Models directory as default. And in contrast, let those "some developers" who don't want their models in the Models directory, move it manually outside the Models directory instead. It's odd having a command |
No. Because for “some developers” like me who is working on a project following DDD naming conventions, our “models” are in sub-directories of a Domain directory (i.e. app/Domain/Catalog). Right now, I can do:
If this command is changed to automatically put classes in a Models directory, then my team and I can no longer make use of it. |
Good luck to other teams (who practice and follow the original MVC structure) who need to organize their Models different than yours. |
Like they can right now? The existing For smaller apps with a handful of models, I’ll just leave them right in |
It still sounds funny using an MVC framework and not following the basic methodology behind it. |
You don’t really need luck to run |
@prezire There's nothing about MVC that requires or even suggests models be under a Models directory. You've made your opinion clear, and it's based on a misunderstanding of what "must" be true in MVC. Further, Laravel doesn't really care to do what MVC or any other pattern requires it to do, but what makes the most sense. |
Based on the number of Likes, fair enough. |
I made a package for this: |
I just released orchestra/canvas which bring As for Laravel, you should be able to customize the configuration using preset: laravel
namespace: App
model:
namespace: App If you want to customize the model namespace, just change it to model:
namespace: App\Model |
For small projects, using the app folder to place eloquent models is OK, but when working with larger projects, this may become messy, since the inclusion of model directories will be necessary. Mixing the functional structure of Laravel with the organizational structure of the modelling part seems not a good practice.
For example:
I have two models, contest and notebook, and they both have a topic related to them. Since both topic are too different to be grouped in a single model, I have distinguished them in the database. But having NotebookTopic and ContestTopic seems wrong, so I create both Notebook and Contest directories. Now I'm mixing organizational directories with structural ones.
Solution:
Creating a folder named Models in the app directory. It may even be a option to add it to the composer psr-4 for quicker reference:
"Model\\": "app/Models"
.The text was updated successfully, but these errors were encountered: