|
1 |
| -# laravel-route-trailing-slash |
| 1 | +# Laravel route trailing slash |
| 2 | + |
| 3 | +Let laravel route work as exactly as how we define it including the trailing slash. |
| 4 | + |
| 5 | +[](./LICENSE) |
| 6 | +[](https://travis-ci.org/xinningsu/laravel-route-trailing-slash) |
| 7 | +[](https://coveralls.io/github/xinningsu/laravel-route-trailing-slash) |
| 8 | +[](https://scrutinizer-ci.com/g/xinningsu/laravel-route-trailing-slash) |
| 9 | +[](https://scrutinizer-ci.com/g/xinningsu/laravel-route-trailing-slash) |
| 10 | +[](https://sonarcloud.io/dashboard?id=xinningsu_laravel-route-trailing-slash) |
| 11 | +[](https://sonarcloud.io/dashboard?id=xinningsu_laravel-route-trailing-slash) |
| 12 | +[](https://sonarcloud.io/dashboard?id=xinningsu_laravel-route-trailing-slash) |
| 13 | +[](https://codeclimate.com/github/xinningsu/laravel-route-trailing-slash/maintainability) |
| 14 | + |
| 15 | +# Background |
| 16 | + |
| 17 | +Currently when we define a route, Laravel will trim all the tailing slashes, output the route url without any tailing slash. When we access an url with trailing slashes, Laravel also will trim them. That makes the tailing slashes meaningless, sometimes it's quite annoying. |
| 18 | + |
| 19 | + |
| 20 | +## Define a route like this |
| 21 | + |
| 22 | +```php |
| 23 | +use App\Http\Controllers\PartnersController; |
| 24 | + |
| 25 | +Route::get('/partners/', [PartnersController::class, 'index'])->name('partners'); |
| 26 | +``` |
| 27 | + |
| 28 | + |
| 29 | +### Current behavior |
| 30 | + |
| 31 | +| URL | Status | |
| 32 | +| ------------- |------------- | |
| 33 | +| [https://laravel.com/partners](https://laravel.com/partners) | 200 | |
| 34 | +| [https://laravel.com/partners/](https://laravel.com/partners/) | 200 | |
| 35 | +| [https://laravel.com/partners////](https://laravel.com/partners////) | 200 | |
| 36 | + |
| 37 | + |
| 38 | +### Expected behavior |
| 39 | + |
| 40 | +| URL | Status | |
| 41 | +| ------------- |------------- | |
| 42 | +| [https://laravel.com/partners](https://laravel.com/partners) | 404 | |
| 43 | +| [https://laravel.com/partners/](https://laravel.com/partners/) | 200 | |
| 44 | +| [https://laravel.com/partners////](https://laravel.com/partners////) | 404 | |
| 45 | + |
| 46 | + |
| 47 | +## Output the route url |
| 48 | + |
| 49 | +```php |
| 50 | +echo route('partners'); |
| 51 | + |
| 52 | +``` |
| 53 | + |
| 54 | +### Current behavior |
| 55 | + |
| 56 | +`https://laravel.com/partners` |
| 57 | + |
| 58 | +### Expected behavior |
| 59 | + |
| 60 | +`https://laravel.com/partners/` |
| 61 | + |
| 62 | + |
| 63 | +## Pagination render |
| 64 | + |
| 65 | +Using database query builder or LengthAwarePaginator/Paginator |
| 66 | + |
| 67 | +### Current behavior |
| 68 | + |
| 69 | +`https://laravel.com/partners?page=2` |
| 70 | + |
| 71 | +### Expected behavior |
| 72 | + |
| 73 | +`https://laravel.com/partners/?page=2` |
| 74 | + |
| 75 | + |
| 76 | +# Usage |
| 77 | + |
| 78 | +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. |
| 79 | + |
| 80 | + |
| 81 | +### RoutingServiceProvider |
| 82 | + |
| 83 | +open `bootstrap/app.php`, right after app instantiation |
| 84 | + |
| 85 | +```php |
| 86 | +$app = new Illuminate\Foundation\Application( |
| 87 | + $_ENV['APP_BASE_PATH'] ?? dirname(__DIR__) |
| 88 | +); |
| 89 | + |
| 90 | +``` |
| 91 | + |
| 92 | +add |
| 93 | +```php |
| 94 | +$app->register(Sulao\LRTS\Routing\RoutingServiceProvider::class); |
| 95 | + |
| 96 | +``` |
| 97 | + |
| 98 | +### PaginationServiceProvider |
| 99 | + |
| 100 | +Pagination Service Provider has to be added **after** laravel original Pagination Service Provider. |
| 101 | + |
| 102 | +add `Sulao\LRTS\Pagination\PaginationServiceProvider::class` to config/app.php under `providers` element, **after** `Illuminate\Pagination\PaginationServiceProvider::class` |
| 103 | + |
| 104 | +```php |
| 105 | +[ |
| 106 | + Illuminate\Pagination\PaginationServiceProvider::class, |
| 107 | + |
| 108 | + // ... |
| 109 | + |
| 110 | + /* |
| 111 | + * Package Service Providers... |
| 112 | + */ |
| 113 | + Sulao\LRTS\Pagination\PaginationServiceProvider::class, |
| 114 | + |
| 115 | + // ... |
| 116 | +]; |
| 117 | + |
| 118 | +``` |
| 119 | + |
| 120 | +That's it. |
| 121 | + |
| 122 | +# License |
| 123 | + |
| 124 | +[MIT](./LICENSE) |
0 commit comments