The Lucid Architecture for Laravel was introduced by Abed Halawi in one of the Laracon event and it grants a laravel app a good way to scale the app in terms of the development flexibility. The architecture follows a Domain Driven design approach.
However, even though Lucid Architecture provides a way you can create a fresh project with Lucid, it's quite difficult to implement on an already running project that wants to try out the Structure. the purpose of this article is to take you through how we can still use Lucid in a well ordered manner without it affecting you app initial setup.
This post is written for as many who will like to implement this clear and simple architecture in their laravel project.
We're going to achieve this in few steps, so follow me tightly:
The Lucid architecture includes some folder structure and files that is required to make things work as expected. In order to make this set up simplified for our already existing project, all we need to do is add this repository as our part of our source, i.e we'll add another remote repository and pull from it (make sure git is already initialized in your project). Here is how we'll do that:
$ git remote add lucid https://github.com/bosunski/lucid-remian.git;
Once this is done, we'll pull from the new remote source to update our project with all the required files and directory structure we need to continue.
We'll pull from the new remote source by running:
$ git pull lucid master --allow-unrelated-histories
After this is done and successful, you should see the src/
folder created with some folders inside of it, and also you'll see autoload.php
file inside
bootstrap
directory of your project, once you've seen these, we're good to go and complete the last steps.
I will explain the reason for the directories and files added via the repository later.
$ composer require lucid-arch/laravel-foundation
The next thing we'll install is the Lucid console application and the reason why we need this is because it helps us in creating scaffolds which includes migrations, controllers, Models etc.
We will install the Lucid console app using this command:
$ composer require lucid-arch/laravel-console
The next thing here is to create an autoload.php
file inside the bootstrap
folder, this folder can be found at the root of your laravel project. This
file is used by the Lucid console application as a means of bootstrapping, so, it's required to run the console application. Good for us the first step already took care
of that, hence, we move to the next step.
Here we'll create an src
at the root of our laravel installation, this folder will contain our services, data, domains etc. Again, this has been take care of in the initial step, so we don
need to do repeat this. The focus is just to explain the need for it.
Lucid expects that the src/
folder be namespaced so that the Lucid console application can work that is why we need to update it at the part where
psr-4
definitions are made, so here's how we'll update that:
{
"psr-4": {
...
"Bash\\": "src/"
...
}
}
Notice how Bash\\
was matched to src/
folder, the console command retrieves the namespace of the src/
folder and uses it in generating controllers, models etc.
We've defined a new namespace so we have to tell composer to register it, we'll do this by simply running:
$ composer dump-autoload
We need to register two service providers here inside config/app.php
, one is a Service provider that registers all Services and the other sets up the things required for our
structure to work as expected. To register them, we'll append them to the providers array inside config/app.php
:
<?php
return [
'providers' => [
//...
Bash\Foundation\ServiceProvider::class,
Lucid\Console\LucidServiceProvider::class,
]
];
Notice how same namespace used inside composer.json
was used above i.e Bash
, make sure that same namespace is used in both cases.
Finally, update src/Foundation/ServiceProvider.php
so that the namespace matches the one registered above.
Once this is done, it means we're done with the set up.
To be sure the Lucid console is working as expected, try creating a service using the Lucid console application by running:
$ ./vendor/bin/lucid make:service Web
You should get something like this:
Service Web created successfully.
Activate it by registering Bash\Services\Web\Providers\WebServiceProvider
in Bash\Foundation\Providers\ServiceProvider@register with the following:
$this->app->register('Bash\Services\Web\Providers\WebServiceProvider');
If all works fine, you should see a folder Web
inside src/Services
already created. You can then follow the instruction to register the service provider as shown above.
Once you do this, serve your project and head to: <your-ip-or-host>/web
and you'll see the Lucid default page created.
That is all I can do to help for now, I really want to see PHP developers adopt clean code while doing their work in Laravel, and that's exactly why this post is written, so that you'll know that at any point in your project, clean code is still achievable.