Routing
Modular, under the hood, uses the Laravel's Routing System, but with some conventions applied, that will make it easier to work with, in Modular Applications.
Types of Routes
After the Modular install, the system will handle three different types of routes out of the box:
1 - App Routes
App Routes are the ones that are available only to logged in users. Ideally, all the navigation links inside the application, after the user logs in, should be App Routes.
The App Routes file for your modules must be called app.php
and must be placed inside the routes
folder of your module. So, for example, if you have a module called Customer
, you should have the following file structure:
modules/Customer
└── routes
└── app.php
These routes will be automatically loaded by the Modular's System with the appropriate middleware to protect the routes, so you don't have to worry about it.
Inside routes/app.php
you can define your routes as you would normally do in Laravel, for example:
<?php
use Illuminate\Support\Facades\Route;
use Modules\Customer\Http\Controllers\CustomerController;
Route::get('customer', [
CustomerController::class, 'index'
])->name('customer.index');
In fact, the Module Generator Command will automatically generate this routes file for you, following the mentioned conventions.
2 - API Routes
Everything is connected, and at some point, it's possible that you will need to create an API for your application. Modular has a built-in way to handle this, and it's very simple, thanks to the Laravel Sanctum package.
If you need to create API routes for your Modules, just create a file called api.php
inside the routes
folder of your Module, and Modular will automatically load it for you. So for example, if you have a Module called Customer
, you should have the following file structure:
modules/Customer
└── routes
├── api.php
└── app.php
The routes defined inside api.php
will have the api
prefix added to them, and be protected by the auth:sanctum
middleware, so you don't have to worry about it. Also, the correct namespace of your Module will be automatically applied to the Controllers, so you don't have to worry about it either.
Inside routes/api.php
, you can define your routes as you would normally do in Laravel, for example:
<?php
use Illuminate\Support\Facades\Route;
use Modules\Customer\Http\Controllers\ApiCustomerController;
// This route will be available at /api/v1/customer
Route::get('/v1/customer', [
ApiCustomerController::class, 'index'
]);
3 - Site Routes
Site Routes are the ones that are publicly available, and can be accessed by anyone, without any type of authentication. These are equivalent to the default Laravel's web.php
routes file.
If your Module needs public routes, for a Landing Page for example, that must save Leads, you can create a file called site.php
inside the routes
folder of your Module, and Modular will automatically load it for you. So for example, if you have a Module called Lead
, you should have the following file structure:
modules/Lead
└── routes
├── app.php
└── site.php
These routes will be loaded without any prefix or namespace applied to them, so you have the freedom to define them as you wish. Inside routes/site.php
, you can define your routes as you would normally do in Laravel, for example:
<?php
use Illuminate\Support\Facades\Route;
use Modules\Lead\Http\Controllers\SiteLeadController;
Route::get('/lead', [
SiteLeadController::class, 'index'
]);
When using app routes
and site routes
in your modules, be careful not to use the same route names and route paths, as they can conflict with each other.