TOPIC COLLECTION
Routing.
A collection of 33 practical ideas to make everyday Laravel work a little smoother.
33 TIPS01Route Group Within a GroupIn Routes, you can create a group within a group, assigning a certain middleware only to some URLs in the "parent" group.Routing02Declare a Resolveroutebinding Method in Your ModelRoute model binding in Laravel is great, but there are cases where we can't just allow users to easily access resources by ID. We might need to verify their…Routing03Assign Withtrashed() to Route::resource() MethodBefore Laravel 9.35 - only for Route::get() Since Laravel 9.35 - also for Route::resource() !Routing04Skip Input NormalizationLaravel automatically trims all incoming string fields on the request. It's called Input Normalization.Routing05Wildcard SubdomainsYou can create route group by dynamic subdomain name, and pass its value to every route.Routing06What's Behind the Routes?If you use Laravel UI package, you likely want to know what routes are actually behind Auth::routes() ?Routing07Route Model Binding: You Can Define a KeyYou can do Route model binding like Route::get('api/users/{user}', function (User $user) { … } - but not only by ID field. If you want {user} to be a…Routing08Route Fallback: When No Other Route Is MatchedIf you want to specify additional logic for not-found routes, instead of just throwing default 404 page, you may create a special Route for that, at the…Routing09Route Parameters Validation with RegexpWe can validate parameters directly in the route, with “where” parameter. A pretty typical case is to prefix your routes by language locale, like fr/blog…Routing10Rate Limiting: Global and for Guests/usersYou can limit some URL to be called a maximum of 60 times per minute, with throttle:60,1Routing11Query String Parameters to RoutesIf you pass additional parameters to the route, in the array, those key / value pairs will automatically be added to the generated URL's query string.Routing12Separate Routes by FilesIf you have a set of routes related to a certain "section", you may separate them in a special routes/XXXXX.php file, and just include it in routes/web.phpRouting13Translate Resource VerbsIf you use resource controllers, but want to change URL verbs to non-English for SEO purposes, so instead of /create you want Spanish /crear , you can…Routing14Custom Resource Route NamesWhen using Resource Controllers, in routes/web.php you can specify - names() parameter, so the URL prefix in the browser and the route name prefix you use…Routing15Eager Load RelationshipIf you use Route Model Binding and think you can't use Eager Loading for relationships, think again.Routing16Localizing Resource UrisIf you use resource controllers, but want to change URL verbs to non-English, so instead of /create you want Spanish /crear , you can configure it with…Routing17Resource Controllers NamingIn Resource Controllers, in routes/web.php you can specify - names() parameter, so the URL prefix and the route name prefix may be different.Routing18Easily Highlight Your Navbar MenusUse Route::is('route-name') to easily highlight your navbar menusRouting19Generate Absolute Path Using Route() HelperPractical Laravel guidance for generate absolute path using route() helper, with examples and copyable PHP code.Routing20Override the Route Binding Resolver for Each of Your ModelsYou can override the route binding resolver for each of your models. In this example, I have no control over the @ sign in the URL, so using the…Routing21If You Need Public URL, But You Want Them to Be SecuredIf you need public URL but you want them to be secured, use Laravel signed URLRouting22Using Gate in Middleware MethodYou can use the gates you specified in App\Providers\AuthServiceProvider in middleware method.Routing23Simple Route with Arrow FunctionYou can use php arrow function in routing, without having to use anonymous function. To do this, you can use fn() = , it looks easier.Routing24Route ViewYou can use Route::view($uri , $bladePage) to return a view directly, without having to use controller function.Routing25Route Directory Instead of Route FileYou can create a /routes/web/ directory and only fill /routes/web.php with: Now every file inside /routes/web/ act as a web router file and you can organize…Routing26Route Resources GroupingIf your routes have a lot of resource controllers, you can group them and call one Route::resources() instead of many single Route::resource() statements.Routing27Custom Route BindingsDid you know you can define custom route bindings in Laravel? In this example, I need to resolve a portfolio by slug. But the slug is not unique, because…Routing28Two Ways to Check the Route NameHere are two ways to check the route name in Laravel.Routing29Route Model Binding Soft-deleted ModelsBy default, when using route model binding will not retrieve models that have been soft-deleted. You can change that behavior by using withTrashed in your…Routing30Retrieve the URL without Query ParametersIf for some reason, your URL is having query parameters, you can retrieve the URL without query parameters using the fullUrlWithoutQuery method of request…Routing31Customizing Missing Model Behavior in Route Model BindingsBy default, Laravel throws a 404 error when it can't bind the model, but you can change that behavior by passing a closure to the missing method.Routing32Exclude Middleware from a RouteYou can exclude middleware at the route level in Laravel using the withoutMiddleware method.Routing33Controller GroupsInstead of using the controller in each route, consider using a route controller group. Added to Laravel since v8.80Routing