skip to Main Content

i am building a multitenant saas system, with laravel for backend and for my frontend i chose to use react js. But it is my first time i do this type of system (Tenant). So i have a question, how should i use tenant router? For example i need to do user management table with react component and i have users route on the tenant.php not on web.php but there is also a api.php On normal app we use router from api.php to interact data from controller to vue or react component. How should i use tenant route on this type of app?

I know i can use route directly from tenant.php but i mean is better to use from api.php ? I am loking for best practise

web.php

Route::redirect('/', 'login');

Auth::routes();

/**
 * Route group middleware
 * 
 */
Route::group(['middleware' => ['auth']], function () {
    // DASHBOARD
    Route::get('/dashboard', [AppHttpControllersHomeController::class, 'index'])->name('home');

    // USERS MANAGEMENTS
    Route::resource('roles', RoleController::class);
    Route::resource('users', UserController::class);
    Route::resource('products', ProductController::class);
});

tenant.php

Route::middleware([
    'web',
    InitializeTenancyByDomain::class,
    PreventAccessFromCentralDomains::class,
])->group(function () {
    // Route::get('/', function () {
    //     return view('auth.login');
    // });
    Route::redirect('/', 'login');

    Auth::routes();

    
    Route::group(['middleware' => ['auth']], function() {
        // DASHBOARD
        Route::get('/dashboard', [AppHttpControllersHomeController::class, 'index'])->name('home');

        // USERS MANAGEMENTS
        Route::resource('roles', RoleController::class);
        Route::resource('users', UserController::class);

api.php


Route::middleware('auth:sanctum')->get('/user', function (Request $request) {
    return $request->user();
});

2

Answers


  1. I think it depends on your application and what do you have, from my point of view if you have APIs for your main application like creating tenants and so on then your solution is better so you can separate the apis meant for your main application and tenant.php is for you tenant APIs
    but if all your APIs are for tenant then you can use api.php while I would still prefer using tenant.php so it can easily adapt if you need app APIs later

    and while adding your tenant.php to RouteServiceProvider you can add it either as web or api based on your preference and needs

    Login or Signup to reply.
  2. I made a new folder in routes and name it tenant. And inside the tenant, I create a new route file for api called api.php and add it to my tenant.php route file.

    Below my tenant.php I add this line
    require DIR.’/tenant/api.php’;

    My tenantapi.php file

    Route::middleware(['api', 'throttle:api',InitializeTenancyByDomain::class,
        PreventAccessFromCentralDomains::class,])
        ->prefix('api/v1')
        ->name('tenant.')
        ->group(function () {
            // Api Routes
        });
    

    From your react side just call api route with your subdomain name.
    For central Domain sass.test
    For tenant domain test.sass.testapiv1users it will point your tenant route for your test tenant

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search