sulimanbenhalim/route-hints

Intelligent 'did you mean?' route suggestions for Laravel 404 errors

1.0.1 2025-08-18 14:56 UTC

This package is auto-updated.

Last update: 2025-08-18 15:11:42 UTC


README

GitHub License PHP Version Support Laravel Version Support

Laravel 404 errors → intelligent route suggestions

Install

composer require sulimanbenhalim/route-hints

Usage

Routes Control

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

Custom Keywords

Route::get('/dashboard', HomeController::class)
    ->withHints(['home', 'control-panel']);

/home → suggests /dashboard

Parameterized Routes

Route::get('/posts/{post}', PostController::class)
    ->includeParameterized(['post' => 'welcome']);

/post → suggests /posts/welcome

Programmatic Access

$suggestions = app('route-hints')->findSuggestions(request());
$suggestions = RouteHints::findSuggestions(request());

Responses

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

Auto-Redirect Setup

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...'
];

Configuration

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

Commands

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

Examples

User Types Gets Suggested Why
/user /users Typo correction
/dashbord /dashboard Spelling mistake
/home /dashboard Custom hint
/product-categories /categories/electronics Parameterized with default

Testing

The package includes a comprehensive test suite:

composer test

Laravel Version Compatibility

Laravel Version Package Version
11.x 1.x
12.x 1.x

Security

If you discover any security issues, please email soliman.benhalim@gmail.com instead of using the issue tracker.

License

The MIT License (MIT). Please see License File for more information.