php - How to use api routes in stancl/tenancy with vue

Solution:

This helped me!

// routes/tenant.php
Route::middleware(['api'])->prefix('api')->group(function () {
   //
   Route::get('test', function () {
       return 'test!';
   });
});

Answer

Solution:

Perhaps a little late to help op but in case others find their way here, I ran into the same issue with version 3 of stancl/tenancy and was able to get it to work by moving my api routes back to routes/api.php and coding them like this...

Route::middleware([
    InitializeTenancyByDomain::class,
    PreventAccessFromCentralDomains::class,
])->group(function () {
    Route::post('/foo', 'App\Http\Controllers\Tenant\FooController@whatever');
    Route::post('/bar', 'App\Http\Controllers\Tenant\BarController@whatever');
});

Answer

Solution:

Just reverse the order of the two functions ( api routes first ), that worked for me!

Like this:

Route::middleware([
    'api',
    InitializeTenancyByDomain::class,
    PreventAccessFromCentralDomains::class,
])->group(function () {

Route::post('/register', [AuthController::class, 'register']);
Route::post('/login', [AuthController::class, 'login']);

Route::apiResource('products', ProductController::class);
Route::apiResource('orders', OrderController::class);
Route::apiResource('orderitems', OrderItemController::class);
   
});

Route::middleware([
    'web',
    InitializeTenancyByDomain::class,
    PreventAccessFromCentralDomains::class,
])->group(function () {
    // Route::get('/', function () {
    //     return 'This is your multi-tenant application. The id of the current tenant is ' . tenant('id');
    // });
    Route::get('{any}', function () {
        return view('layouts.app');
    })->where('any', '.*');
    Route::Resource('allproducts', ProductController::class); // tried using it like this and returned blank html page
    
    Auth::routes();
});

Source