An open API service indexing awesome lists of open source software.

https://github.com/sulimanbenhalim/route-hints

Laravel 404 errors → intelligent route suggestions
https://github.com/sulimanbenhalim/route-hints

404 laravel middleware package php routing suggestions ux

Last synced: 10 days ago
JSON representation

Laravel 404 errors → intelligent route suggestions

Awesome Lists containing this project

README

          

# RouteHints Laravel Package

[![GitHub License](https://img.shields.io/github/license/sulimanbenhalim/route-hints)](https://github.com/sulimanbenhalim/route-hints/blob/main/LICENSE.md)
[![PHP Version Support](https://img.shields.io/badge/php-%3E%3D%208.2-blue)](https://www.php.net/supported-versions.php)
[![Laravel Version Support](https://img.shields.io/badge/laravel-%3E%3D%2011.0-red)](https://laravel.com/docs/11.x/releases)

> Laravel 404 errors → intelligent route suggestions

## Install

```bash
composer require sulimanbenhalim/route-hints
```

## Usage

### Routes Control
All routes discoverable by default

```php
Route::get('/users', UserController::class); // ✓ Suggested
Route::get('/posts', PostController::class)->discoverable(); // ✓ Explicitly suggested
Route::get('/admin', AdminController::class)->hidden(); // ✗ Never suggested
```

### Custom Keywords
```php
Route::get('/dashboard', HomeController::class)
->withHints(['home', 'control-panel']);
```
`/home` → suggests `/dashboard`

### Parameterized Routes
```php
Route::get('/posts/{post}', PostController::class)
->includeParameterized(['post' => 'welcome']);
```
`/post` → suggests `/posts/welcome`

### Programmatic Access
```php
$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`:
```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:**
```php
// 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

```bash
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

```bash
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:
```bash
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](LICENSE.md) for more information.