Looking to hire Laravel developers? Try LaraJobs

laravel-route-macros maintained by laravel-working-group

Description
Collection of useful shortcuts as macros on the Route facade
Last update
2023/08/21 19:39 (dev-main)
License
Downloads
24
Tags

Comments
comments powered by Disqus

Laravel Route Macros

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.