Laravel 404 errors → intelligent route suggestions
composer require sulimanbenhalim/route-hints
All routes discoverable by default
Route::get('/users', UserController::class); // ✓ Suggested
Route::get('/posts', PostController::class)->discoverable(); // ✓ Explicitly suggested
Route::get('/admin', AdminController::class)->hidden(); // ✗ Never suggested
Route::get('/dashboard', HomeController::class)
->withHints(['home', 'control-panel']);
/home
→ suggests /dashboard
Route::get('/posts/{post}', PostController::class)
->includeParameterized(['post' => 'welcome']);
/post
→ suggests /posts/welcome
$suggestions = app('route-hints')->findSuggestions(request());
$suggestions = RouteHints::findSuggestions(request());
Request Type | Response |
---|---|
HTML | Default Laravel 404 view with clickable suggestions |
JSON | {"suggestions": [{"url": "http://app.test/users"}]} |
Auto-redirect | Direct redirect when similarity > threshold |
Required for session-based auto-redirect
Add to bootstrap/app.php
:
->withMiddleware(function (Middleware $middleware): void {
$middleware->prepend(\Illuminate\Session\Middleware\StartSession::class);
})
// Or running this command, mentioned blow in the docs
// php artisan route-hints:setup-session
Auto-redirect Data:
// Query method adds URL parameters:
// /users?route_corrected_from=%2Fuser&similarity=90.5&route_name=users.index
// Session method stores structured data:
session('route_hints_correction') = [
'original_path' => '/user',
'corrected_path' => '/users',
'similarity_percentage' => 90.5,
'route_name' => 'users.index',
'corrected_at' => '2024-01-15T...'
];
php artisan vendor:publish --tag=route-hints-config
All Settings
Setting | Default | Description |
---|---|---|
enabled |
true |
Enable/disable route hints |
max_distance |
5 |
Levenshtein distance limit |
max_suggestions |
3 |
How many to show |
default_discoverable |
true |
Routes discoverable by default |
excluded_patterns |
[] |
Never suggest these routes |
cache_ttl |
3600 |
Cache discoverable routes (seconds) |
show_similarity_percentage |
false |
Show similarity % in responses |
show_route_names_json |
false |
Include route names in JSON |
show_route_names_view |
false |
Show route names in HTML |
auto_redirect.enabled |
false |
Auto-redirect feature |
auto_redirect.threshold |
80 |
Similarity % needed for redirect |
auto_redirect.method |
session |
How to pass correction info |
auto_redirect.query_param |
route_corrected_from |
Query parameter name |
auto_redirect.session_key |
route_hints_correction |
Session key name |
php artisan route-hints:analyze [path] # Test route suggestions for any path
php artisan route-hints:cache build # Build route cache for performance
php artisan route-hints:cache status # Check cache status and info
php artisan route-hints:cache clear # Clear route cache
php artisan route-hints:setup-session # Configure auto-redirect session
User Types | Gets Suggested | Why |
---|---|---|
/user |
/users |
Typo correction |
/dashbord |
/dashboard |
Spelling mistake |
/home |
/dashboard |
Custom hint |
/product-categories |
/categories/electronics |
Parameterized with default |
The package includes a comprehensive test suite:
composer test
Laravel Version | Package Version |
---|---|
11.x | 1.x |
12.x | 1.x |
If you discover any security issues, please email soliman.benhalim@gmail.com instead of using the issue tracker.
The MIT License (MIT). Please see License File for more information.