Quick tip for laravel routes:

Code example

I’m writing this because I’ve watched a few videos on youtube where people have added routes and route groups and all this stuff into the routes file. And it quickly starts to look messy.

This is to present another way to help keep route files clean and specific.

For example: if you need authed routes then you could create a new file routes/authed.php and add a function to your app/Providers/RouteServiceProvider.php file

* Define the “authed” routes for the application.
* These routes all receive session state, CSRF protection, etc.
* @return void
protected function mapAuthedRoutes()
Route::middleware([‘web’, ‘authed’])

Just make sure to append $this->mapAuthedRoutes(); to the end of the map function in that file so that the function is called.

As you can see you can specify middlware, prefixes, as, and really any other setting here. That way in your routes file it’ll be clean and simple with just routes in it.