https://github.com/stechstudio/laravel-route-context
https://github.com/stechstudio/laravel-route-context
Last synced: 11 months ago
JSON representation
- Host: GitHub
- URL: https://github.com/stechstudio/laravel-route-context
- Owner: stechstudio
- License: mit
- Created: 2022-01-06T18:11:56.000Z (over 4 years ago)
- Default Branch: main
- Last Pushed: 2022-01-06T19:59:53.000Z (over 4 years ago)
- Last Synced: 2025-04-02T14:27:17.495Z (about 1 year ago)
- Language: PHP
- Size: 3.91 KB
- Stars: 0
- Watchers: 3
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE.md
Awesome Lists containing this project
README
# Route context in Laravel
[](https://packagist.org/packages/stechstudio/laravel-route-context)
This is a super small package that enables you to provide additional context to your routes. Any context variables you specify will be treated as route parameters.
## Installation
You know the drill...
```bash
composer require stechstudio/laravel-route-context
```
## Usage
The idea is that sometimes you want to reuse a controller method or Livewire fullpage component, while providing additional context at the routing layer.
Imagine you need to list support tickets, and you have a controller and view that handles this. You have multiple endpoints where tickets might be displayed in a slightly different manner.
With this package you can specify additional context right alongside your routes like this:
```php
Route::get('tickets', [TicketController::class, 'index']);
Route::get('tickets/archived', [TicketController::class, 'index'])->with([
'archived' => true
]);
Route::get('tickets/mine', [TicketController::class, 'index'])->with([
'user' => fn() => auth()->user()
]);
Route::get('tickets/{user}', [TicketController::class, 'index']);
```
Now in your `TicketController` you can inject your context variables, just as if they had been route parameters:
```php
public function index(User $user, $archived = false) {
$tickets = Tickets::query()
->when($user->exists, fn($q) => $q->where('user_id', $user->id))
->when(!$archived, fn($q) => $q->whereNull('archived_at'))
->paginate();
}
```
Notice that context values can be callbacks, if you need it evaluated after your app has bootstrapped, session is started, auth is available, etc.
## License
The MIT License (MIT). Please see [License File](LICENSE.md) for more information.