Simplify your Laravel project with a folder-by-feature structure
By default, Laravel stores files in a folder-by-type structure:
app/
┣ Http/
┃ ┗ Controllers/
┃ ┣ CustomerController.php
┃ ┣ OrderController.php
┃ ┣ ProductController.php
┗ Models/
┣ Customer.php
┣ Order.php
┣ Product.php
This folder structure works ok for small projects, but as soon as you write more and more controllers, models, etc. it gets very hard to find the actual file you are looking for.
So I like to structure my project in a folder-by-feature structure:
app/
┣ Customer/
┃ ┣ Customer.php
┃ ┗ CustomerController.php
┣ Order/
┃ ┣ Order.php
┃ ┗ OrderController.php
┗ Product/
┣ Product.php
┗ ProductController.php
Much cleaner!
To use this folder structure in a new Laravel project, follow these steps:
1. Remove everything inside the /app folder
rm -r ./app/*
2. Replace the default container bindings in /bootstrap/app.php
…or bind these to your own Kernel/Handler implementations.
3. Create your first feature
artisan make:provider App\\Product\\ProductServiceProvider
4. Register the service provider in /config/app.php
5. Bootstrap your feature in the service provider
For more info about Service Providers, see https://laravel.com/docs/providers
6. Build your amazing feature!
You can add everything you like to the feature folder, from database migrations to event listeners. Split the code to the extend that suits your project!