Home > OS >  Laravel Route namespace problems with the backslash
Laravel Route namespace problems with the backslash

Time:12-21

I want to use the Laravel 9 route namespace function and I wonder how to pass the namespace correctly?

Route::as('some.')->namespace('App\Http\Controllers\Api\V1')->group(function() {
    Route::post('/store', [SomeController::class, 'store'])
        ->name('store');
});

I get the following error message:

    "message": "Target class [SomeController] does not exist.",
    "exception": "Illuminate\Contracts\Container\BindingResolutionException",

Question: How do I do this properly?

But this works

Route::as('some.')->group(function() {
    Route::post('/store', [\App\Http\Controllers\Api\V1\SomeController::class, 'store'])
        ->name('store');
});

CodePudding user response:

Well, you need to provide the right path to the controller file. For example at the top of the file put this: use App\Http\Controllers\Controller;

CodePudding user response:

use it :

Route::as('some.')->namespace('Api\V1')->group(function() {
    Route::post('/store', [SomeController::class, 'store'])
        ->name('store');
});

CodePudding user response:

Since you are using a namespace for route,so you should use

Route::as('some.')->namespace('App\Http\Controllers\Api\V1')->group(function() {
    Route::post('/store', 'SomeController@store')
        ->name('store');
});

Ref: https://laravel.com/docs/8.x/releases#routing-namespace-updates

  • Related