kamranahmedse / Laravel Modular Boilerplate
Laravel Modular Boilerplate
A Laravel 5.* boilerplate for creating modular applications.
An article explaining how it was created how can you use it can be found through this blog post Creating a Modular Application in Laravel 5.1
Setup
Please follow the below steps to setup the application
- Clone the repository
git clone http://github.com/kamranahmedse/laravel-modular-boilerplate
- Install dependencies via composer
composer install
- Create a
.env
file, maybe by duplication.env.example
which is already there. - Generate the key:
php artisan key:generate
- The application is ready. A dummy module called
ModuleOne
has already been added to give you an idea about how it works. The module has some routes, a controller and a dummy model as well. Considering that you are serving the repository viaphp artisan serve
at port8000
you can access the dummy routes placed inapp/Modules/ModuleOne/routes.php
via:
localhost:8000/module-one
localhost:8000/module-one/model-test
Adding your Modules
-
Create a directory with your module name e.g.
MyAwesomeModule
inside theapp/Modules
directory. Inside this directory create a file calledroutes.php
for the routes and create the directoriesControllers
,Models
andViews
for your controllers, models and views respectively. -
Open up the file
config/module.php
. This is the file where you will be registring your modules. Add your module name in themodules
array like this:
return [
'modules' => [
'ModuleOne',
'MyAwesomeModule'
]
];
-
Inside the
app\Modules\MyAwesomeModule\Controllers
directory, create your controllers with the namespace set toApp\Modules\MyAwesomeModule\Controllers
. In the same way, create the models insideapp\Modules\MyAwesomeModule\Models
directory with the namespace set toApp\Modules\MyAwesomeModule\Models
. And create the view files in theapp\Modules\MyAwesomeModule\Views
directory. Everything i.e. Controllers, models and views in this module work the same, however while using views you will have to specify your views likeMyAwesomeModule::viewName
i.e. you will have to prepend the modulename with the resolution to the start of how you normally access a view. -
For a detailed how-to, I would recommend you to read this article especially this part. Also you can go through the existing module i.e.
ModuleOne
which has been added just for your reference.