-
Notifications
You must be signed in to change notification settings - Fork 11.1k
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
Routes Provided by Package Do Not Load When Middleware Applied #50992
Comments
Try applying the middleware through a route group within the service provider: Route::group([
'middleware' => 'auth',
], function () {
$this->loadRoutesFrom(__DIR__ . '/../../routes/web.php');
}); Does that help? |
I'm assuming the public function boot(): void
{
Route::group([
'middleware' => 'auth',
], function () {
$this->loadRoutesFrom(__DIR__ . '/../../routes/web.php');
});
$this->loadMigrationsFrom(__DIR__ . '/../../database/migrations');
$this->loadViewsFrom(__DIR__ . '/../../resources/views', 'eppLogin');
} |
Yeah, the facade. Hmm this is a weird one. Would appreciate some external help here if anyone has any insights. |
Thank you for reporting this issue! As Laravel is an open source project, we rely on the community to help us diagnose and fix issues as it is not possible to research and fix every issue reported to us via GitHub. If possible, please make a pull request fixing the issue you have described, along with corresponding tests. All pull requests are promptly reviewed by the Laravel team. Thank you! |
I'll try to take a deeper dive into this sometime this week. Might be able to submit a PR if I find something. I will say my skill may not be up to par for Laravel Core however. 🤓 |
@driesvints I dove a bit deeper into this and determined where the redirect to
|
TBH: can't you just remove the |
No such luck. Same outcome. The only thing I've done that makes this work is commenting out the route registration in the package and adding It's a bit of a hacky solution, but it does allow the route to work with the middleware properly. I don't think this is the right solution, however; it feels like it's breaking a lot of Laravel rules. |
@onairmarc Are you sure, it's v11? I tried as you described and cannot reproduce the bug. Furthermore, the HOME constant is not present anywhere in the fresh install (with vendors ofc). I could try to help you with this, but if you could provide a sample repository with a reproducible error, that would be very helpful. |
@sxtnmedia Yes, I just double checked and I did comment out <?php
use Illuminate\Auth\Middleware\Authenticate;
use Illuminate\Auth\Middleware\RedirectIfAuthenticated;
use Illuminate\Foundation\Application;
use Illuminate\Foundation\Configuration\Exceptions;
use Illuminate\Foundation\Configuration\Middleware;
return Application::configure(basePath: dirname(__DIR__))
->withProviders()
->withRouting(
web: base_path('routes/web.php'),
api: base_path('routes/api.php'),
commands: base_path('routes/console.php'),
// channels: __DIR__.'/../routes/channels.php',
health: '/up',
)
->withMiddleware(function (Middleware $middleware) {
$middleware->redirectGuestsTo(fn() => route('login'));
$middleware->redirectUsersTo('/dashboard');
$middleware->throttleApi();
$middleware->alias([
'auth' => Authenticate::class,
'guest' => RedirectIfAuthenticated::class,
]);
})
->withExceptions(function (Exceptions $exceptions) {
//
})->create(); |
Heya, thanks for reporting. We'll need more info and/or code to debug this further. Can you please create a repository with the command below, commit the code that reproduces the issue as one separate commit on the main/master branch and share the repository here? Please make sure that you have the latest version of the Laravel installer in order to run this command. Please also make sure you have both Git & the GitHub CLI tool properly set up. laravel new bug-report --github="--public" Please do not amend and create a separate commit with your custom changes. After you've posted the repository, we'll try to reproduce the issue. Thanks! |
@driesvints Sure thing! I'll try to get this done this afternoon. |
You can load routes like this: Route::middleware($this->middlewareRoute)
->namespace($this->namespace)
->group(__DIR__.$this->routePath); |
@driesvints Here is the link to the repo: https://github.com/onairmarc/package-routing-issue Reproduction Steps:
Looking forward to your feedback. |
@milwad-dev I am not familiar with this method of loading routes via packages. Can you please elaborate? |
I managed to recreate it through your repo. I have no idea what's going on but your code looks like it should work to me. I do not know why you get redirected instead of the route being shown especially since you're already logged in. Would greatly appreciate some additional help/insights with this one. |
Yay! I'm not completely crazy! 😂 I'm happy to help where I can, but I'm unsure where to begin looking for a solution. As I continue my research, I'll post any relevant findings here. |
I think the best point to start debugging is in the auth middleware to see what the state is when you hit the package route. Obviously the middleware thinks you're not logged in at that point. |
I've added this to my to-do list. Should have some time this weekend to do a deep dive. |
Your package route is never placed in the Route::middleware(['web', 'auth'])->get('/package', function () {
return 'Package route...';
}); |
Urgh, bashing myself for missing this obvious one 😅 |
Laravel Version
11.3
PHP Version
8.3.3
Database Driver & Version
No response
Description
Routes do not load properly when a middleware is applied in a package.
PackageServiceProvider (package_root/src/Providers/PackageServiceProvider.php)
Web Routes (package_root/routes/web.php)
When the routes are registered with middleware via a package, the auth middleware redirects the request to
AppServiceProvider::HOME
, but when the exact same routes are copied and pasted intoroutes/web.php
in the main application, the routes function as expected. Removing the middleware when registering the routes in the package allows the routes to function properly, except for the fact that the middleware is no longer applied.Steps To Reproduce
AppServiceProvider::HOME
routes/web.php
. The route works as expected.The text was updated successfully, but these errors were encountered: