laravel / nova-issues

554 stars 34 forks source link

Getting "Method Not Allowed" on saving/updating any resource #3734

Closed JackJohansson closed 2 years ago

JackJohansson commented 2 years ago

Description:

I'm working on an app with 2 different user types / guards. Recently I've noticed that I no longer can save, update or create any resource via nova. Every attempt results in the following error:

There was a problem submitting the form : "Method Not Allowed"

I ran php artisan route:list, and I noticed that the method actually is registered:

| GET|HEAD         | nova-api/{resource}  |   | Laravel\Nova\Http\Controllers\ResourceIndexController@handle   | nova |
| POST             | nova-api/{resource}  |   | Laravel\Nova\Http\Controllers\ResourceStoreController@handle   | nova |

I commented my entire route files to see if it helps, but it didn't. I'm also able to log out and into nova dashboard, so the guard should be working fine.

The app is being developed on local, but the same applies to the production version.

Here are some of the configs that might help:

app.php

'url' => env( 'APP_URL', 'http://app.test')

auth.php

'defaults' => [
    'guard'     => 'employee',
    'passwords' => 'employees',
],
'guards' => [
    'employee' => [
        'driver'   => 'session',
        'provider' => 'employees',
    ],
    'client' => [
        'driver'   => 'session',
        'provider' => 'clients',
    ],
    'sanctum'  => [
        'driver'   => 'sanctum',
        'provider' => 'clients',
    ],
],
'providers' => [
    'employees' => [
        'driver' => 'eloquent',
        'model'  => App\Models\Employee::class,
    ],
    'clients' => [
        'driver' => 'eloquent',
        'model'  => App\Models\Client::class,
    ],
]

cors.php

'paths' => [ 'api/*', 'nova-api/*', 'sanctum/csrf-cookie' ],
'allowed_methods' => [ '*' ],
'allowed_origins' => [ '*' ],
'allowed_origins_patterns' => [],
'allowed_headers' => [ '*' ],
'exposed_headers' => [],
'max_age' => 0,
'supports_credentials' => TRUE,

nova.php

'guard'        => env( 'NOVA_GUARD', 'employee' ),
'passwords' => env( 'NOVA_PASSWORDS', 'employees' ),

sanctum.php

'guard'   => 'client',
'stateful' => 'app.test'

I also tried commenting out all of my custom exception handlers, and auth service providers to see if there's something wrong there, but no progress.

crynobone commented 2 years ago

Please provide full reproducing code for the issue.

JackJohansson commented 2 years ago

So I had to replace all the files in my app to find out which file was causing the issue. It appears that it was this line:

$route = \Illuminate\Support\Facades\Route::getRoutes()->match( request() )?->getName();

This line is inside a middleware that it assigned to the $middleware property inside Kernel.php. If I move this to the web middleware group, the issue is solved, but parts of the app break.

It's worth mentioning that I've bound the class that holds the line as a singleton to the service container, and I'm type-hinting its name inside the middleware's constructor. Then, I call a method that holds the above line.

Any ideas why this is causing an issue with nova?

crynobone commented 2 years ago

As mentioned earlier, will need reproducing code to debug this.

JackJohansson commented 2 years ago

Just put that line in the handle method of "ANY" middleware registered in the middelware property of Kernel.php.

stale[bot] commented 2 years ago

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

JackJohansson commented 2 years ago

The issue is related to Laravel itself, but it drastically impacts Nova's ability to to save models.

Since this thread is getting ignored, for anyone else who encounters this issue later, they can move the Route::getRoutes()->match( ... ) line to the "web" middleware group.