Handling domains/subdomains and grouping routes into functions in Laravel

Here I wanted to show you two very interesting things and with this several added things that would be how to manage your application in Laravel with sub Domains or also domains you can manage it in the same way since it is indifferent and the other thing is the advantages that we have to group here in our routes as you are seeing that I have a function here that simply has a lot of grouped routes So that is what I want to talk about and more or less you will see why I am dealing with all this in a video right now that you see my implementation this is obviously the Academy project that I have of the Free development as you want to see it and we are going to talk a little bit about what I am doing as I have told you there I have three modules:

  1. The one from Academia, which is the website for courses and books
  2. I also have the dashboard one
  3. And I also have the blog

For this, well, for the Academy, as you can see, it is 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');
}

Domains and subdomains

So here we are going to see how the operation works. Here we have it, it is so simple, a type of route called domain in which we can place either a domain or a subdomain, which is exactly what I am placing here, as you can see, I simply place the subdomain here and little else to say. In the end, this is how it is in Vue, so what I do is the little trick in quotes, I place optional elements here to precisely manage the routing:

Route::get('academia/{n1?}/{n2?}/{n3?}/{n4?}/{n5?}/{n6?}', [AcademyController::class, 'index'])->name('course-vue');

That is to say, for example, here I go to books or a detail:

https://academy.desarrollolibre.net/libros

https://academy.desarrollolibre.net/libro/visor/laravel/

Enter the above path as optional parameters.

Also note another interesting point that reminds us that even if the application is developed, I have to develop it locally, for this reason I get very annoyed creating subdomains and so on, so I manage it in the classic way, which would be to directly ask about the environment if we are in development or production to use the domain or subdomain scheme or directly a route

Admin

Here you can place either a domain or a subdomain in case you are wondering how you can manage subdomains I can tell you that well That obviously depends on your Hosting in your Hosting you surely have a subdomain option there and for the rest that subdomain is managed as if it were a domain therefore whatever you are that exists for the domain you have to replicate it with the subdomain in my particular case I had or in hostinger what you have to do is always point to the public folder therefore that was the same configuration that I did here for the subdomain simply point to the public folder and little else since in the end Laravel is in charge of defining the routes here for us here as I told you there is not much to do because the entire application is in Vue therefore here I only have a single route.

function routesDashboard()
{
   Route::get('/', function () {
       return redirect()->route("post-list");
   });
***
}

Grouping routes into functions

Let's see the admin for example the admin does make a little more sense because it is the administration part that is to say the list of category posts or the categories post cl books tutorials classes the bunch of things that I have there and therefore they are a set of routes here again when using the subdomains that is to say in the development environment I want the as who says the normal thing that is to simply place in my test domain Slash admin but here also in production I use a subdomain for the dashboard therefore here it does make sense that I use this type of grouping through functions for example instead of placing all the routes here since this in the end if we see it is simply a bunch of routes that is to say instead of placing all this directly and replicating on both sides I simply create a function with all the advantages that this brings us in which that bunch of routes are found and I simply use them here they are normal routes simply with Group routes of type get post p pad delete everything that you want to create the same thing that we do in the Laravel course in the basic Laravel course or all the Arabic courses or anything in Arabic that you do out there are simply routes and in this way you can easily reuse them, obviously you can place them in any other file that you consider in case you want to maintain a better organization you can create here I don't know eh web dashboard web em tutorial and or web directly blog or web academy whatever and you simply import them and use them But well for me since there are not so many routes I don't handle it this way either and here I can group them easily and I simply invoke them where I consider just as you are seeing in this case based on the same condition that I presented to you before for the Academy if we are in production I want to use the subdomain and the same thing that redirects me to the subdomain is placed admin And if I am in development then well I don't care I use everything from the same test domain so with that I presented to you the scheme that I use to manage subdomains or domains and also the grouping of routes that makes all the sense in the world especially when you are working with subdomains and domains and you also want to keep your normal scheme simple when you are developing the application.

- Andrés Cruz

En español

Andrés Cruz

Develop with Laravel, Django, Flask, CodeIgniter, HTML5, CSS3, MySQL, JavaScript, Vue, Android, iOS, Flutter

Andrés Cruz In Udemy

I agree to receive announcements of interest about this Blog.