Laravel route file cleanup

[av_heading tag=’h3′ padding=’10’ heading=’Quick tip for laravel routes:’ color=” style=” custom_font=” size=” subheading_active=” subheading_size=’15’ custom_class=” admin_preview_bg=” av-desktop-hide=” av-medium-hide=” av-small-hide=” av-mini-hide=” av-medium-font-size-title=” av-small-font-size-title=” av-mini-font-size-title=” av-medium-font-size=” av-small-font-size=” av-mini-font-size=”][/av_heading]

[av_image src=’http://turtlebytes.com/wp-content/uploads/2018/01/Selection_005-1213×430.png’ attachment=’120′ attachment_size=’featured’ align=’center’ styling=” hover=’av-hover-grow’ link=’lightbox’ target=” caption=” font_size=” appearance=” overlay_opacity=’0.4′ overlay_color=’#000000′ overlay_text_color=’#ffffff’ animation=’no-animation’ admin_preview_bg=”][/av_image]

[av_textblock size=” font_color=” color=” av-medium-font-size=” av-small-font-size=” av-mini-font-size=” admin_preview_bg=”]
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’])
->namespace($this->namespace)
->group(base_path(‘routes/authed.php’));
}

[/av_textblock]

[av_textblock size=” font_color=” color=” av-medium-font-size=” av-small-font-size=” av-mini-font-size=” admin_preview_bg=”]
Just make sure to append $this->mapAuthedRoutes(); to the end of the map function in that file so that the function is called.
[/av_textblock]

[av_textblock size=” font_color=” color=” av-medium-font-size=” av-small-font-size=” av-mini-font-size=” admin_preview_bg=”]
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.
[/av_textblock]

[av_hr class=’default’ height=’50’ shadow=’no-shadow’ position=’center’ custom_border=’av-border-thin’ custom_width=’50px’ custom_border_color=” custom_margin_top=’30px’ custom_margin_bottom=’30px’ icon_select=’yes’ custom_icon_color=” icon=’ue808′ av-desktop-hide=” av-medium-hide=” av-small-hide=” av-mini-hide=”]

[av_iconlist position=’left’ iconlist_styling=” custom_title_size=” custom_content_size=” font_color=” custom_title=” custom_content=” color=” custom_bg=” custom_font=” custom_border=” av-medium-font-size-title=” av-small-font-size-title=” av-mini-font-size-title=” av-medium-font-size=” av-small-font-size=” av-mini-font-size=” admin_preview_bg=”]
[av_iconlist_item title=’https://laravel.com/docs/5.5/routing’ link=’manually,https://laravel.com/docs/5.5/routing’ linktarget=’_blank’ linkelement=” icon=’ue8ba’ font=’entypo-fontello’][/av_iconlist_item]
[av_iconlist_item title=’https://github.com/laravel/laravel/blob/master/app/Providers/RouteServiceProvider.php’ link=’manually,https://github.com/laravel/laravel/blob/master/app/Providers/RouteServiceProvider.php’ linktarget=’_blank’ linkelement=” icon=’ue8ba’ font=’entypo-fontello’][/av_iconlist_item]
[/av_iconlist]

Leave a Comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Scroll to Top