laravel-working-group/laravel-route-macros

Collection of useful shortcuts as macros on the Route facade

1.0.1 2023-08-21 15:39 UTC

This package is auto-updated.

Last update: 2025-03-21 19:11:04 UTC


README

Adds new methods/shortcuts to the Route facade

Installation

Install via composer:

composer require laravel-working-group/laravel-route-macros

Methods

viewWithBindings

Like Route::view('/uri', 'view', $data) but with the route parameters already bound to data just like implicit model binding would do in other cases:

Route::viewWithBindings('/user/{user}', 'view', bindings: [ 'user' => \App\Models\User::class ]);
// blade: {{ $user->name }}

with

Like Model::with()->find() but for routes with implicit model binding where you would otherwise have to $model->load()/$model->loadMissing() in the callback/controller action afterwards. Instead, now you can do this:

Route::get('/users/{user}', [ UserController::class, 'show' ])->with('user', 'followers');

file

Display files (e.g. static HTML pages) directly in browser where you'd otherwise have to use the File/Storage facade inside a callback/controller action.

Route::file('/.well-known/security.txt', 'static-pages/security.txt');

whereEnum

>= v9.x

Wrapper for Route::where() working similarly to Route::whereIn() but with enum support (passing enum directly instead of array).

< v9.x

Wrapper for Route::where() working similarly to Route::whereNumeric() or Route::whereAlpha() without having to specify the or-chained RegExp manually.

Syntax

Route::get('/imports/{format}', [ ImportController::class, 'import' ])->withEnum('format', ImportFormat::class);

viewWithDataFrom

Route::viewWithDataFrom('users', 'users', [ 'users' => [ \App\Models\User, 'all' ] ]);

Contributing

If you have more methods to be added, feel free to add an issue or a merge request.