stancl / tenancy-docs

stancl/tenancy docs & website
https://tenancyforlaravel.com/docs
MIT License
66 stars 636 forks source link

Issue with Laravel Nova Tools Integration (API Requests to Backend) #281

Closed HAL0594 closed 2 months ago

HAL0594 commented 2 months ago

Issue with Laravel Nova Tools Integration (API Requests to Backend)

Laravel Nova Version: 4
Laravel Version: 10.48.22
Tenancy for Laravel Version: 3

Description

I recently started using the Tenancy package for an integration with Laravel Nova 4. I followed the steps in the official guide:

The connection with the domains and databases works well for Nova Resources. However, I'm encountering issues with API routes related to the Tools in my application.

My API routes are configured as follows in routes/api.php:

<?php
use Illuminate\Support\Facades\Route;
use Stancl\Tenancy\Middleware\InitializeTenancyByDomain;
use Stancl\Tenancy\Middleware\PreventAccessFromCentralDomains;

Route::name('api.')->group(function () {
    foreach (File::allFiles(__DIR__ . '/api') as $partial) {
        require_once $partial->getPathname();
    }
});

Broadcast::routes(['middleware' => ['auth:sanctum', InitializeTenancyByDomain::class, PreventAccessFromCentralDomains::class]]);

For instance, if I had a file with routes in the /api directory (e.g., /api/tool.php), it would look like this:

<?php
use Illuminate\Support\Facades\Route;
use App\Http\Controllers\ToolController;
use Stancl\Tenancy\Middleware\InitializeTenancyByDomain;
use Stancl\Tenancy\Middleware\PreventAccessFromCentralDomains;

Route::middleware([
    'auth:sanctum',
    InitializeTenancyByDomain::class,
    PreventAccessFromCentralDomains::class,
])->group(function () {
    Route::post('/tool/abc', [ToolController::class, 'functionABC']);
});

For example, in the Laravel Nova Tools, I typically use Axios to make requests to the backend:

await axios
    .post('/api/tool/abc', {
        input: '123',
    })
    .then((response) => {
        let info = response.data;
        this.form = info.form;
    })
    .catch((error) => {})
    .finally(() => {});

Error

After integrating the Tenancy package, the API request no longer returns a response. Instead, my tools show the following error:

"message": "Target class [2] does not exist.",
    "exception": "Illuminate\\Contracts\\Container\\BindingResolutionException",
    "file": "/var/www/html/vendor/laravel/framework/src/Illuminate/Container/Container.php",
    "line": 914,
    "trace": [
        {
            "file": "/var/www/html/vendor/laravel/framework/src/Illuminate/Container/Container.php",
            "line": 795,
            "function": "build",
            "class": "Illuminate\\Container\\Container",
            "type": "->"
        },

I also added InitializeTenancyByDomain::class and PreventAccessFromCentralDomains::class to my API and web route groups in kernel.php, as well as to the middlewares in nova.php, but I still encountered the same issue.

Does anyone have any idea why this error occurs when making a request with Axios? Or is there an additional step, such as registering the provider or middleware, to make it work with the Tools? I appreciate any help in advance.

stancl commented 2 months ago

This repo is for contributing to the docs.