victor mazeli
victor mazeli

Reputation: 141

How to use api routes in stancl/tenancy with vue

Good day, i am working on Saas product and am making use of the stancl/tenancy package. i have configured and set up my app to work with the package, but i am unable to access my api routes to my vue frontend. i moved my routes to the routes/tenant.php file. and still don't get any data response back instead i get a html content type response with no data. my app works fine without the stancl/tenancy package. please any help or advice on how i can resolve this issue? below is my routes/tenant.php file:

<?php

declare(strict_types=1);

use Illuminate\Support\Facades\Route;
use Stancl\Tenancy\Middleware\InitializeTenancyByDomain;
use Stancl\Tenancy\Middleware\PreventAccessFromCentralDomains;
use App\Http\Controllers\ProductController;
use App\Http\Controllers\OrderController; 
use App\Http\Controllers\OrderItemController;
use App\Http\Controllers\API\AuthController;

/*
|--------------------------------------------------------------------------
| Tenant Routes
|--------------------------------------------------------------------------
|
| Here you can register the tenant routes for your application.
| These routes are loaded by the TenantRouteServiceProvider.
|
| Feel free to customize them however you want. Good luck!
|
*/

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();
});

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);
   
});

Upvotes: 2

Views: 3079

Answers (3)

Mohammed Bensouna
Mohammed Bensouna

Reputation: 107

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();
});

Upvotes: 1

Husmukh
Husmukh

Reputation: 76

This helped me!

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

Upvotes: 4

bz13
bz13

Reputation: 71

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');
});

Upvotes: 3

Related Questions