Volex
Volex

Reputation: 775

Laravel 10 - UnexpectedValueException - Invalid route action, how to setup route namespace in route namespace

I have upgraded my Laravel application to version 10 and encountered an issue with setting up a route with a namespace. In previous versions of Laravel, I used the following code in my routes/web.php file to set up a route with a namespace:

enter image description here

But in Laravel 10, this doesn't seem to work anymore. I get an "Invalid route action" error with the namespace included in the action parameter.

enter image description here

I've managed to fix my first route on the picture with this code, and now this route works well:

Route::namespace('App\Http\Controllers\Main')->group(function () {
    Route::get('/', IndexController::class)->name('main.index');
});
Route::namespace('App\Http\Controllers\Admin')->prefix('admin')->group(function () {
    Route::namespace('App\Http\Controllers\Admin\Main')->group(function () {
        Route::get('/', App\Http\Controllers\Admin\Main\IndexController::class)->name('main.index');
    });
});

   Auth::routes();   

But I don't know how to fix second route with namespace in namespace.

Can anyone provide guidance on how to properly set up a route with a namespace in Laravel 10? Thank you in advance!

P.S I'm using __invoke() method in my controllers

namespace App\Http\Controllers\Main;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;

class IndexController extends Controller
{
    public function __invoke()
    {
        return view('main.index');
    }
}

Upvotes: 0

Views: 708

Answers (1)

bassxzero
bassxzero

Reputation: 5041

I think the namespaces are additive. The first one should be 'App\Http\Controllers\Admin' and the second should be 'Main'

E.G

Route::namespace('App\Http\Controllers\Admin')->prefix('admin')->group(function () {
    Route::namespace('Main')->group(function () {
        Route::get('/', App\Http\Controllers\Admin\Main\IndexController::class)->name('main.index');
    });
});

Upvotes: 1

Related Questions