Download the PHP package xinningsu/laravel-route-trailing-slash without Composer
On this page you can find all versions of the php package xinningsu/laravel-route-trailing-slash. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download xinningsu/laravel-route-trailing-slash
More information about xinningsu/laravel-route-trailing-slash
Files in xinningsu/laravel-route-trailing-slash
Package laravel-route-trailing-slash
Short Description Let laravel route work as exactly as how we define it including the trailing slash.
License MIT
Informations about the package laravel-route-trailing-slash
Laravel route trailing slash
Let laravel route work as exactly as how we define it including the trailing slash.
Background
Currently when we define a route, Laravel will trim all the trailing slashes, output the route url without any trailing slash. When we access an url with trailing slashes, Laravel also will trim them. That makes the trailing slashes meaningless, sometimes it's quite annoying.
Define a route like this
Output the route url
Current behavior
Expected behavior
Pagination render
Using database query builder or LengthAwarePaginator/Paginator
Current behavior
Expected behavior
Installation
There are two service providers, as the router service provider has to register at the very beginning before laravel http kernel class instantiation, so I can not make it work by Laravel Package Auto-Discovery function. Have to manually add it.
RoutingServiceProvider
open bootstrap/app.php
, right after app instantiation
add
PaginationServiceProvider
Pagination Service Provider has to be added after laravel original Pagination Service Provider.
add Sulao\LRTS\Pagination\PaginationServiceProvider::class
to config/app.php under providers
element, after Illuminate\Pagination\PaginationServiceProvider::class
That's it, now we can generate url with/without trailing slash, same as the definition of route.
Mismatch action
Currently Laravel will trim trailing slashes of access url, so no matter how many trailing slashes the url has, it makes no difference.
Current behavior
URL | Status |
---|---|
https://laravel.com/partners | 200 |
https://laravel.com/partners/ | 200 |
https://laravel.com/partners//// | 200 |
Now we can custom the mismatch action, such as abort 404, or 301/302 redirect, if route mismatch only because of trailing slash.
404 action
open app/Providers/AppServiceProvider.php
, add below line to register
method
Now the behavior became
URL | Status |
---|---|
https://laravel.com/partners | 404 |
https://laravel.com/partners/ | 200 |
https://laravel.com/partners//// | 404 |
301/302 action
open app/Providers/AppServiceProvider.php
, add below line to register
method
Now the behavior became
URL | Status |
---|---|
https://laravel.com/partners | 301 redirect to https://laravel.com/partners/ |
https://laravel.com/partners/ | 200 |
https://laravel.com/partners//// | 301 redirect to https://laravel.com/partners/ |
License
MIT