Search code examples
phplaravelroutesmulti-tenantlaravel-7

Switching routes on subdomain, laravel multitenancy


Morning! I'm programming a laravel 7 multitenancy project using hyn package version 5.6 For now i have a principal app (localhost) and a subdomain (tecnotienda.localhost). But when i route them it ussually overlaps, i mean: it just shows the principal or just shows the subdomain. This is my routes/web.php

<?php

use Illuminate\Support\Facades\Route;
use App\Http\Controllers\ReportController;


/*
|--------------------------------------------------------------------------
| PRIMERA PARTE: RUTAS DEL ADMINISTRADOR
|--------------------------------------------------------------------------
|
| Las rutas a continuación, corresponden a unas rutas por default y las rutas 
| de autenticación para el admin de la aplicación principal
|
*/


Route::get('/',function(){
  return view('index');
});
// ruta al enviar correo
Route::post('/send', 'ControllerMail@send');

Route::get('/home', 'HomeController@index')->name('home');


Route::post('user/logout','Auth\LoginController@userLogout')->name('user.logout');

Route::prefix('admin')->group(function(){
    //admin dashboar
    Route::get('/','AdminController@index')->name('admin.dashboard');

    //login
    Route::get('/login','Auth\AdminLoginController@showLoginForm')->name('admin.login');
    Route::post('/login','Auth\AdminLoginController@login')->name('admin.login.submit');

    //logout
    Route::post('/logout','Auth\AdminLoginController@logout')->name('admin.logout');

    //register
    Route::get('/register','Auth\AdminRegisterController@showRegistrationForm')->name('admin.register');
    Route::get('/register','Auth\AdminRegisterController@register')->name('admin.register.submit');
    /*
    |--------------------------------------------------------------------------
    | ADMINISTRACIÓN DE LOS TENANTS
    |-------------------------------------------------------------------------
    |
    */
    Route::get('/tenants','TenantController@index')->name('admin.tenant.index');
    Route::get('/tenants/new-tenant','TenantController@showForm')->name('admin.tenant.show');
    Route::post('/tenants/done','TenantController@create')->name('admin.tenant.create');
    Route::get('/tenants/{id}','TenantController@edit')->name('admin.tenant.edit');
    Route::post('/tenants/{record_id}/update', 'TenantController@update')->name('admin.tenant.update');
    
    Route::get('/posts/{id}','TenantController@deactivate')->name('admin.tenant.deactivate');
    //sofdelete


      /*
    |--------------------------------------------------------------------------
    | REPORTES
    |-------------------------------------------------------------------------
    |
    */
    Route::get('/reports','ReportController@index')->name('admin.report.index');

    //algo raro pasa aca
    Route::get('/reports/pdf','ReportController@pdf');


});



//tenant
Route::domain('tecnotienda.localhost')->group(function () {
  Route::get('/', function () {
    return view('tecnotienda.welcome');
});

// Login Routes
Route::get('login', 'Auth\LoginController@showLoginForm')->name('login');
Route::post('login', 'Auth\LoginController@login');
Route::post('logout', 'Auth\LoginController@logout')->name('logout');

});

I'm trying using a EnforceTenancy middleware just like this page https://www.seismicpixels.com/creating-a-laravel-saas-framework-part-3 but...it has no effect

Resume: i need to group routes by subdomain give by tenant fqdn. Can you help me?


Solution

  • It is one of the solutions, there are also other solutions possible but if you are only using one route change as per domain so it will be quick & easy.

    Route::get('/', function (\Illuminate\Http\Request $request) {
       if(strpos($request->getHost(), 'trial.local.com') === 0){ 
           return "sub"; 
       }else{
           return view('welcome'); 
       }
    });