Skip to content

[9.x] Fix Duplicate Route Namespace#41021

Merged
taylorotwell merged 1 commit into
laravel:9.xfrom
moisish:route-namespace
Feb 15, 2022
Merged

[9.x] Fix Duplicate Route Namespace#41021
taylorotwell merged 1 commit into
laravel:9.xfrom
moisish:route-namespace

Conversation

@moisish

@moisish moisish commented Feb 15, 2022

Copy link
Copy Markdown
Contributor

When you are using group namespaces you cannot use the group controller method or single action controllers.
Also you cannot use the controller class as a parameter on your route

Route::namespace($module->namespace().'\App\Http\Controllers')->group($module->getRoutes());

This works

Route::get('/', 'FrameworkController@index')->name('index');

The following scenarios are not working

Controller group

use Modules/Foo/App/Http/Controllers/FooController;
Route::prefix('foo')
    ->controller(FooController::class)
    ->group(function () {
        Route::get('/foo', 'foo');
    });

Single Action Controller

use Modules/Foo/App/Http/Controllers/InvokableController;
Route::prefix('foo')->group(function () {
        Route::get('/foo', InvokableController::class);
    });
use Modules/Foo/App/Http/Controllers/FooController;

Route::prefix('foo')->group(function () {
        Route::get('/', [FooController::class, 'index']);
    });

The solution I came up with is to not prepend the group namespace if the controller class starts with the same namespace

@moisish
moisish marked this pull request as ready for review February 15, 2022 08:28
@driesvints

driesvints commented Feb 15, 2022

Copy link
Copy Markdown
Member

If you use controller classes in your route it's intended to not set the namespace. That's why it's disabled by default in the skeleton. You either use one or the other.

I see there's no docs for this but that's because we're trying to move people away from this setting and use class referenced controllers more.

@moisish

moisish commented Feb 15, 2022

Copy link
Copy Markdown
Contributor Author

I get that it's disabled but is there a reason not being flexible no matter if you set a namespace?

@moisish

moisish commented Feb 15, 2022

Copy link
Copy Markdown
Contributor Author

When you have namespaced groups you cannot use the new controller method or even invokable controllers which I think we should allow to.

Main reason we have namespaced groups is when used with modules/domains which means including multiple route files.
Then we can check the namespace of the current route Route::getCurrentRoute()->getAction('namespace') for whatever reason

Invokable controllers should not be affected by group namespaces tho

@driesvints

Copy link
Copy Markdown
Member

When you have namespaced groups

That's what I'm trying to say: I think we removed this from the docs to no longer promote namespaced groups.

@taylorotwell

Copy link
Copy Markdown
Member

This has come up a few times and I guess I am willing to try merging this. It will break applications that literally have the namespace repeated intentionally (App\Http\Controllers\App\Http\Controllers\FooController) but that of course seems strange. Will see how it goes - may have to revert this later.

@taylorotwell
taylorotwell merged commit 2ff6066 into laravel:9.x Feb 15, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants