I have created over 10 Laravel projects from scratch in the last year, and worked on more than 25 and I have seem one common thing in all of them.
The Laravel default directory structure only works when your project it's really small.
As the project grows, the default directory structure becomes a problem because it does not scale properly.
After a few use cases, models, policies, and so on, you will end with something similar to this:
app/
├── Actions
│ ├── Actionable.php
│ ├── Graph
│ │ └── ...
│ ├── MonthlyResume
│ │ └── GenerateMonthlyResumeAssets.php
│ ├── User
│ │ └── UpdateUser.php
│ └── VoidActionable.php
├── Console
│ ├── Commands
│ │ ├── CalculateUserCryptoPortfolioBalanceCommand.php
│ │ ├── CategorizeTransactionsIaCommand.php
│ │ ├── GenerateMonthlyReportAssetsCommand.php
│ │ ├── IdeHelperAllCommand.php
│ │ ├── PruneNotificationsCommand.php
│ │ ├── SendMonthlyReportNotificationCommand.php
│ │ ├── SendNewUpdateChangeLogNotificationCommand.php
│ │ └── TrainCategorizationIaCommand.php
│ └── Kernel.php
├── Contracts
│ └── Pipe.php
├── Events
│ ├── SharingConfigurationSaved.php
│ ├── SupportMessageCreated.php
│ └── VisitIncreased.php
├── Exceptions
│ ├── GetBankAccountTransactionError.php
│ ├── Handler.php
│ └── RenderableApiError.php
├── Http
│ ├── Controllers
│ │ ├── Banks
│ │ ├── Controller.php
│ │ ├── CryptoCurrency
│ │ ├── Notification
│ │ ├── SupportMessage
│ │ └── User
│ │ ├── DeleteUserController.php
│ │ ├── GetUserController.php
│ │ └── PutUserController.php
│ ├── Kernel.php
│ ├── Middleware
│ │ ├── Authenticate.php
│ │ ├── DefaultLocale.php
│ │ ├── TrustProxies.php
│ │ ├── ValidateSignature.php
│ │ └── VerifyCsrfToken.php
│ ├── Requests
│ │ ├── PostSupportMessageRequest.php
│ │ └── PutUserRequest.php
│ └── Resources
├── Jobs
│ ├── CalculateUserCryptoPortfolioBalanceJob.php
│ ├── GenerateMonthlyResumeAssetsJob.php
│ ├── GetBankAccountDetails.php
│ └── GetCoinGeckoValuesJob.php
├── Listeners
│ ├── SendAdminNotificationOnSupportMessageCreated.php
│ ├── SendEmailToUserOnVisitIncremented.php
│ └── SendMailOnSupportMessageCreated.phpphp
├── Mail
│ ├── SupportMessage
│ └── Visits
├── Models
│ ├── Bank.php
│ ├── SharingConfiguration.php
│ ├── SupportMessage.php
│ ├── User.php
│ └── UserCryptoPortfolioBalance.php
├── Notifications
│ ├── Notifiable.php
│ ├── ThrottledNotification.php
│ ├── UncategorizedTransactionNotification.php
│ └── UpdateChangeLogNotification.php
├── Policies
│ └── CryptoCurrencyOrderPolicy.php
├── Providers
│ ├── AppServiceProvider.php
│ ├── EventServiceProvider.php
│ ├── HorizonServiceProvider.php
│ └── RouteServiceProvider.php
├── Services
│ └── CategorizationAi.php
└── Support
├── OrderType.php
├── SafeNumberFromStringParser.php
├── ShortNumberFormatter.php
├── StockAssets
└── helpers.php
I removed a lot of files and directories from tree, but you can imagine that it's impossible to see all the code related to users.
As you can see, it's really difficult to know what's happening there. You are working in a users controller, for example, and you don't have all the relevant classes close to it.
You can't see, at a glance, all the relevant classes and models for your current use case.
The main key, to properly scale your app, is to being able to see al the related code to the current use case you're working on at a glance.
For example, if you're working on the user registration use case, you should have the user model, user policies, registration request, the registered event, and more, close to you.
src/
├── Leads
├── Teams
└── Users
├── Actions
│ ├── CreateUser
│ │ ├── CreateUser.php
│ │ └── CreateUserAction.php
│ ├── LoginUserAction.php
│ ├── LogoutUserAction.php
│ └── RefreshAuthenticationTokenAction.php
├── Authenticatable.php
├── Authentication.php
├── Events
│ └── UserRegistered.php
├── Http
│ ├── Controllers
│ │ └── Api
│ │ ├── PostAuthenticationLoginApiController.php
│ │ ├── PostAuthenticationLogoutApiController.php
│ │ ├── PostAuthenticationRefreshApiController.php
│ │ └── PostAuthenticationRegisterApiController.php
│ ├── Requests
│ │ ├── PostAuthenticationLoginRequest.php
│ │ └── PostUserRequest.php
│ └── routes
│ └── api.php
├── Infrastructure
│ └── UserEventServiceProvider.php
├── InvalidCredentials.php
├── Listeners
│ └── SendWelcomeEmailOnUserRegistered.php
├── Mail
│ └── WelcomeMail.php
└── User.php
But know, you're wondering, how can I achieve this architecture easily on Laravel.
The start point is composer.json
file. We are going to create a new folder ./src
to store all our new code.
Adding the directory to the psr-4
key in the composer.json
we are going to autoimport all our files easily.
"autoload": {
"psr-4": {
"App\\": "app/",
"DeveloperJoy\\": "src/",
// ...
},
// ...
}
Just with this, you can put all your models, controllers, requests, policies, and more, in a custom domain folder under src/User
, for example.
There are only a few things missing: routes, and providers, so let's go one by one.
For routes we have multiple options, for me, the most simpler and easier is to import them in the web.php
or in the api.php
file.
I usually write a helper file inside routes folder that looks like this:
<?php
function domain_web_routes(string $domain): void
{
require __DIR__."/../src/{$domain}/Http/routes/web.php";
}
function domain_api_routes(string $domain): void
{
require __DIR__."/../src/{$domain}/Http/routes/api.php";
}
and imported in my composer.json file just adding it to the files array.
"autoload": {
// ...
"files": [
"routes/helpers.php"
]
},
A them, in my default web.php
or api.php
I just import each new domain folder when it has any route.
This is, for example, my web.php
file:
<?php
use Illuminate\Support\Facades\Route;
Route::inertia('/', 'Welcome');
domain_web_routes('Leads');
Inside src/Leads/Http/routes
y have a web.php
file with all my routes related to leads.
Is really that simple.
With providers is actually pretty easy.
We have a file under bootstrap/app.php
that has an array of all our providers.
You just have to add new providers to it, and it will be processed automatically on load time.
return Application::configure(basePath: dirname(__DIR__))
->withProviders([
DeveloperJoy\Users\Infrastructure\UserEventServiceProvider::class,
])
->create();
Do you own a company or need help with your Laravel project? I can help you with that. Check the plans and let me know if you have any questions.
Get 1 month free with yearly plan during checkout.
$ 3 ,995
/m
What's included:
$ 5 ,995
/m
What's included:
Ok, just book a call now.
Good question! For starters, the annual cost of a full-time senior-level developer now exceeds $100,000, plus benefits (and good luck finding one available).
Aside from that, you may not always have enough work to keep them busy at all times, so you're stuck paying for time you aren't able to utilize.
With the monthly plan, you can pause and resume your subscription as often as you need to ensure you're only paying your developer when you have work available for them.
No, once subscribed you're able to request as many things as you'd like, and they will be delivered one by one.
Delivery will always be fast without compromising quality. Very complex requests will be divided into smaller products for continuous delivery.
Always, before starting to work, we can make a planning so that you know, before starting, when each change will be delivered.
You'll work directly with me, the founder and only person behind DeveloperJoy.
We understand you may not have enough work to fill up entire month. Perhaps you only have one or two requests at the moment. That's where pausing your subscription comes in handy.
Billing cycles are based on 31 day period. Let's say you sign up and use the service for 21 days, and then decide to pause your subscription. This means that the billing cycle will be paused and you'll have 10 days of service remaining to be used anytime in the future
I had experience making products with PHP, Go, and JavaScript/TypeScript.
Also I feel really comfortable working with WordPress, Laravel, Symfony, Vue.js, React, Svelte, and more.
Mail me and we will see how best to collaborate.
You can make this request by email, sharing a task board with me or, depending on your plan, by call or Slack.
That's fine. You can pause your subscription when finished and return when you have additional requests needs. There's no need to let the remainder of your subscription go to waste.
Due to the high quality nature of the work, there will be no refunds issued.