framework: Routes Provided by Package Do Not Load When Middleware Applied
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)
<?php
namespace Example\Package\Providers;
use Illuminate\Support\ServiceProvider;
class ExamplePackageProvider extends ServiceProvider
{
public function register(): void
{
}
public function boot(): void
{
$this->loadRoutesFrom(__DIR__ . '/../../routes/web.php');
$this->loadMigrationsFrom(__DIR__ . '/../../database/migrations');
$this->loadViewsFrom(__DIR__ . '/../../resources/views', 'eppLogin');
}
}
Web Routes (package_root/routes/web.php)
<?php
use Example\Package\Http\Controllers\DemoController;
Route::middleware('auth')->group(function () {
Route::get('/dummy_package_route, [DemoController::class, 'index'])
->name('dummy_package_route_name.index');
});
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 into routes/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
- Install a package that registers a route wrapped in the auth middleware via the package. (a simple dummy or symlinked/composer path package will work. (that’s what I have))
- Attempt to visit that route, you will be redirected to
AppServiceProvider::HOME
- Comment out the routes registered via the package and place the same routes with the middleware in
routes/web.php
. The route works as expected.
About this issue
- Original URL
- State: closed
- Created 3 months ago
- Comments: 21 (9 by maintainers)
Urgh, bashing myself for missing this obvious one 😅
Your package route is never placed in the
web
middleware group, so sessions are never started.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.
@driesvints Sure thing! I’ll try to get this done this afternoon.