L11 slim-skeleton request path are too prescriptive #50156
Replies: 4 comments 5 replies
-
Thank you for this. There is a saying. If it works fine, leave it. |
Beta Was this translation helpful? Give feedback.
-
We are curious what was the need that triggered this change... We also faced problems with the trim strings middleware and we had to move it from global to api and web routes, which is easy fix (except for 11 as can be seen). |
Beta Was this translation helpful? Give feedback.
-
Here is my solution to these problems. Initially, all middleware is under the hood. In return Application::configure(basePath: dirname(__DIR__))
->withRouting(function () {
Route::middleware('web')->group(base_path('routes/web.php'));
// Route::middleware('api')->prefix('api.')->group((base_path('routes/api.php'));
})
->withMiddleware(function (Middleware $middleware) {
//
})
->withExceptions(function (Exceptions $exceptions) {
//
})->create(); The methods protected $middleware = [
// ...
];
protected $middlewareGroups = [
'web' => [
// ...
],
'api' => [
// ...
],
// ...
]; However, this file can be rewritten (just as in Laravel 11 it can be done with configuration files). This will solve problem №3. We can enter a command like this: php artisan route:publish After that, we will get a copy of this file in our project, which the framework will use instead of the one in the kernel. Again, the system is similar to the configuration files. |
Beta Was this translation helpful? Give feedback.
-
"Developer needs to know these groups under the hood" We can document them. "Why are these groups more important?" They aren't. But they are the typical, conventional groups used by 99%+ of Laravel applications. Define your own groups if you want. "You can't remove a middleware or replace one" $middleware->web(remove: TrimStrings::class)
$middleware->web(replace: [TrimStrings::class => CustomTrimStrings::class]) Heck, you can even just copy the entire web middleware group over so you can see everything just like Laravel 10: $middleware->use([
\Illuminate\Http\Middleware\TrustProxies::class,
\Illuminate\Http\Middleware\HandleCors::class,
\Illuminate\Foundation\Http\Middleware\PreventRequestsDuringMaintenance::class,
\Illuminate\Http\Middleware\ValidatePostSize::class,
\Illuminate\Foundation\Http\Middleware\TrimStrings::class,
\Illuminate\Foundation\Http\Middleware\ConvertEmptyStringsToNull::class,
]);
$middleware->group('web', [
\Illuminate\Cookie\Middleware\EncryptCookies::class,
\Illuminate\Cookie\Middleware\AddQueuedCookiesToResponse::class,
\Illuminate\Session\Middleware\StartSession::class,
\Illuminate\View\Middleware\ShareErrorsFromSession::class,
\Illuminate\Foundation\Http\Middleware\ValidateCsrfToken::class,
\Illuminate\Routing\Middleware\SubstituteBindings::class,
]); |
Beta Was this translation helpful? Give feedback.
-
Ahead of the release of laravel 11, there is an increasing amount of discussion in the community about its slim-skeleton. Personally, despite my mixed feelings at the beginning, I think that the new structure better reflects the work of the framework. However, one problem still stands out strongly. And it is related to the non-obviousness of the request path.
Previously, laravel behaved like a real framework - it defined the execution path, providing callbacks to determine the user to describe the logic. The request path was obvious. It was possible to understand that global middleware came first, and their list was visible. Then there are middlewareGroups and their list was also visible. But the most important thing was obvious: there are no other middleware. Any problem with the request led to a visible middleware list.
The same problem with packing groups of routes. By introducing a new abstraction "web/api/channels/etc." and hiding their middleware in the core of the framework, we face a lot of problems:
Yes, "Hello, world" has become easier. However, developers creating more complex software got a non-consistent interface and problems with redefining existing groups.
These problems could be solved by creating a command to publish a file that would be similar to what was previously in the kernel and RouteServiceProvider. Or pack all underhood bindings direct to app.php. This will allow developers in complex projects to rewrite the routes and middleware defined by the framework, and will also help beginners to see exactly what stages the request is going through.
What do you think about it?
UPD: I have been trying out Laravel 11 for about a week now, updating training materials for people who have not worked with Laravel before. And it's just unrealistic to explain where all these routes and middleware come from, if you don't dig into the core of the framework. I'm not sure that the current solution will help beginners at all.
7 votes ·
Beta Was this translation helpful? Give feedback.
All reactions