visit
At we collect constructive feedback, boiled down to single arguments, recognize duplicates and merge them into movements. In this blog post, we show a way to split up large Laravel applications into smaller micro-services, in our case Laravel & Lumen applications, and the advantages and pitfalls it brings with it. As a result, we sped up our applications by more than 30% and achieved greater maintainability, too. These principles can, of course, be easily applied to other frameworks.
stomt component While Routes and Controllers are staying in the HTTP-layer, components include all the business logic which is separated in (in the meantime called “Jobs” in Laravels language), resulting and their handlers as well as entities/models/repositories. Component folders can also have sub-components. Components can also communicate with each other. Example: Posts on our web platform are called “stomts”. Let’s say stomts can have comments. You would create a sub-component called “Comment” and place it into the Stomt-component folder. The comment-folder would have the same structure of sub-folders.
Basic idea: The apps folder contains multiple Laravel/Lumen instances.
Anything related to deployments: ElasticBeanstalk files, configs, migrations and seeds, environment-files and scripts are based in the root directory of the backend. The shared folder contains our shared components and service providers. We will dig into this soon.
Release.sh for example automatically creates a new git flow release including semantic versioning and regeneration our API documentation via .
Update.sh calls dependency updating composer commands in all services/apps.
Now let’s dive one step deeper.
Take a close look at the api-application folder. We are always referencing to the root .env-file while using symlinks to reference the configs and database folders within the root-folder. This way configs can be easily maintained across all services as each config file allows multiple configurations. This may be Laravel specific. You can also see what we store in our shared folder: Components, Providers and Services. We will explore how to make service providers shareable later on. Let us first take a look on how to make the shared-folder accessible by the proper use of and .
Now, what do we do about namespaces? Composer makes it pretty easy to define PSR-4 namespaces. While each of our application has its own Namespace (e.g. StomtApi for our REST API) and Configfiles don’t need them, our Components folder has an own Namespace called Stomt.
We make those files accessible by using composers autoload configuration:Pretty easy, hm? Right now we are always deploying the full monolithic codebase while having the advantage of slim and more maintainable applications. More performance by being able to use Lumen for our and the full fledged Laravel framework when needed.
There is a small pitfall if you want to make ServiceProviders accessible between Laravel and Lumen applications. They both extend a different EventServiceProvider.
Lumen extends from:Laravel\Lumen\Providers\EventServiceProviderWhile Laravel extends from:Illuminate\Foundation\Support\Providers\EventServiceProvider
But if you take a look inside, both are doing exactly the same thing. Laravel is using the Event facade while Lumen is using app(‘events’) to obtain an Event-instance because in Lumen, Facades are disabled by default. To ensure that the ServiceProvider system still works, Lumen makes use of app(‘events’). As the Lumen way would also work in Laravel, the solution is obvious. We’ll copy Lumens EventServiceProvider into the shared Providers-folder and rename the file into “ServiceProvider”. Then we let all Service Providers extend our new ServiceProvider. (You can’t call it “EventServiceProvider” as you normally use the EventServiceProvider to register in your applications)
In general: If sharing service providers makes sense totally depends on your applications. For us it does and therefore we wanted to share how this can be done.Originally published at .