fsasvari / laravel-trailing-slash
The package that adds redirection with trailing slash to Laravel framework.
Installs: 145 219
Dependents: 0
Suggesters: 0
Security: 0
Stars: 59
Watchers: 3
Forks: 18
Open Issues: 4
Requires
- php: ^8.2
- illuminate/routing: ^11.0|^12.0
Requires (Dev)
- illuminate/database: ^11.0|^12.0
- mockery/mockery: ^1.6
- overtrue/phplint: ^9.5
- phpunit/phpunit: ^11.0
This package is auto-updated.
Last update: 2025-02-21 11:46:15 UTC
README
Adds url formatting and redirection with trailing slash to Laravel framework versions 12.x, 11.x, 10.x, 9.x, 8.x, 7.x, 6.x and 5.x.
Compatibility Chart
Installation
Step 1: Install package
To get started with Laravel Trailing Slash, use Composer command to add the package to your composer.json project's dependencies:
composer require fsasvari/laravel-trailing-slash
Or add it directly by copying next line into composer.json:
"fsasvari/laravel-trailing-slash": "7.*"
Step 2: Service Provider
If you are using Laravel 11.x
and above, register the LaravelTrailingSlash\RoutingServiceProvider
in your bootstrap/providers.php
configuration file:
return [ // Package Service Providers... // ... LaravelTrailingSlash\RoutingServiceProvider::class, // ... ],
If you are using Laravel 10.x
and below, register the LaravelTrailingSlash\RoutingServiceProvider
in your config/app.php
configuration file:
'providers' => [ // Application Service Providers... // ... // Package Service Providers... // ... LaravelTrailingSlash\RoutingServiceProvider::class, // ... ],
Step 3: .htaccess
If you are using apache, copy following redirection code from public/.htaccess
to your own project:
<IfModule mod_rewrite.c>
# Redirect To Trailing Slashes If Not A Folder Or A File...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !(/$|\.)
RewriteRule (.*) %{REQUEST_URI}/ [R=301,L]
</IfModule>
Step 4: Routes
In routes/web.php, you must use routes with trailing slashes now:
Route::get('/', function () { return view('welcome'); }); Route::get('about/', function () { return view('about'); }); Route::get('contact/', function () { return view('contact'); });
Usage
Every time you use some Laravel redirect function, trailing slash ("/") will be applied at the end of url.
return redirect('about/'); return back()->withInput(); return redirect()->route('text', ['id' => 1]); return redirect()->action('IndexController@about');
Change log
Please see CHANGELOG for more information what has changed recently.
Notice
There is a problem with overriding Laravel Paginator
and LengthAwarePaginator
classes. So, every time you use paginate()
method on your models, query builders etc., you must set current path for pagination links. Example:
$texts = Text::where('is_active', 1)->paginate(); $texts->setPath(URL::current()); $texts->links();
Licence
MIT Licence. Refer to the LICENSE file to get more info.
Author
Frano Šašvari
Email: sasvari.frano@gmail.com