Laravel set application scope and Admin Route

Written by Jigesh Raval on April 15, 2019

Setting up the admin route and application scope will help application to understand the current scope which will then be used for code splitting. I.e Separate Admin and Front Views, Separate Admin and Front Controllers, Separate Admin and Front Route files.

This will keep an application organized and easy to maintain in the long run.

Initially, when I started using Laravel for one of the application, I realized the codebase was increased drastically and it was a harrowing experience to find and correct my own code from the large pool.

To be precise, sometimes I could not understand whether this controller is being called in the admin section or in the front view? This could be attributed to poor function naming, however, that was not the case, to be honest.

Okay, let’s start doing it.

1. Laravel Distinct/Separate Views for Admin or Backend and Frontend

Step 1: Open config/app.php and add two new keys “app_scope” and “admin_route”

Laravel Separate Views for Admin or Backend and Frontend

By default, this value should be “front” for the key “app_scope”.

Step 2: Open app/Providers/AppServiceProvider.php

  1. In this file first, use App\Http\Request at the top of the file
  2. Add the following code in function boot

As you can see in the above code that, we have set app.app_scope in the configuration which can be used globally throughout the application.

So next, we will split our Views and Controllers folders accordingly and we will call different route file, i.e admin.php for admin routes and web.php for front routes.

Keep reading our article to know how to separate views for admin and backend in Laravel application