Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[9.x] Fix Duplicate Route Namespace #41021

Merged
merged 1 commit into from
Feb 15, 2022
Merged

Conversation

moisish
Copy link
Contributor

@moisish moisish commented Feb 15, 2022

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
Copy link
Member

driesvints commented Feb 15, 2022

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
Copy link
Contributor Author

moisish commented Feb 15, 2022

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

@moisish
Copy link
Contributor Author

moisish commented Feb 15, 2022

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
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
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