Handling domains/subdomains and grouping routes into functions in Laravel
Content Index
In this section, I want to show you two interesting things for managing your Laravel application, and not just the Laravel Authorization system with Gates and Policies that we presented earlier, which is ideal for granting access through subdomains depending on the user profile:
- How to work with subdomains or domains.
- How to group routes to maintain a clean and reusable scheme.
We'll look at this using the Academy project I have at Desarrollo Libre as an example, where I have three main modules:
- The Academy one, which is the course and book website
- I also have the dashboard one
- And I also have the blog
For this, well, for the Academy one, as you can see, it's a subdomain, and I also manage the dashboard with a subdomain:
if (config('app')['app_route'] == 'production') {
Route::domain('academy.desarrollolibre.net')->group(function () {
Route::get('/{n1?}/{n2?}/{n3?}/{n4?}', [AcademyController::class, 'index'])->name('course-vue');
});
Route::get('/academia', function () {
return redirect('https://academy.desarrollolibre.net/');
});
} else {
Route::get('academia/{n1?}/{n2?}/{n3?}/{n4?}/{n5?}/{n6?}', [AcademyController::class, 'index'])->name('course-vue');
}Laravel allows us to use the domain route type, where we can specify a domain or subdomain. In this case, academy.desarrollolibre.net is the subdomain that we point to Laravel's public folder.
Domains and Subdomains
Locally, to avoid complicating things by creating subdomains, I manage the same project with normal routes and conditionals based on the development or production environment.
Examples of routes that work with optional parameters:
https://academy.desarrollolibre.net/libros
https://academy.desarrollolibre.net/libro/visor/laravel/
This allows any route to enter through the optional parameters defined in the route.
Laravel takes care of managing the routes internally, so there isn't much more to configure.
So, here we're going to see how the operation is. Here we have such a simple route type called domain in which we can place either a domain or a subdomain, which is exactly what I'm placing here, just as you can see. I simply put the subdomain here and there's not much more to say really. In the end, since this is for Vue, the trick, so to speak, I use is placing optionals here to precisely handle the routing:
Route::get('academia/{n1?}/{n2?}/{n3?}/{n4?}/{n5?}/{n6?}', [AcademyController::class, 'index'])->name('course-vue');Notice another interesting point: remember that I also have to develop the application, so locally, I find it annoying to constantly create subdomains and such. Therefore, I handle it the classic way, which would be to directly ask here about the environment (whether we are in development or production) to use the scheme of domains or subdomains, or directly a simple route.
Grouping Routes in Functions
For the dashboard or administration panel, grouping routes makes more sense because multiple functionalities exist:
function routesDashboard()
{
Route::get('/', function () {
return redirect()->route("post-list");
});
***
}By grouping routes into functions, we gain several advantages:
- Reusability: We can use the same routes in different environments without duplicating code.
- Organization: We can separate routes by module (e.g., web/dashboard, web/blog, web/academy).
- Flexibility: In production, we can use subdomains, and in development, a local domain without complex changes.
Example of usage in production vs. development
- Production: We use subdomains to separate the modules.
- Development: Everything is managed from a test domain to simplify the workflow.
This allows you to maintain a simple scheme locally and take advantage of the subdomain structure in production; to further customize your application with specific access depending on the role, permissions, etc., the next step is to learn how to manage user preferences.
I agree to receive announcements of interest about this Blog.
We'll talk about how you can group your routes into domains and how you can group to reuse routes across functions.