Laravel set application scope and Admin Route

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"

'app_scope' => 'front', 'admin_route' => 'admin'config/app.php

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
/** * Bootstrap any application services. * * @return void */ public function boot(Request $request) { $path_array = $request->segments(); $admin_route = config('app.admin_route'); //If URL path is having "admin" then mark the current scope as Admin if (in_array($admin_route, $path_array)) { config(['app.app_scope' => 'admin']); } $app_scope = config('app.app_scope'); //if App scope is admin then define View/Theme folder path if ($app_scope == 'admin') { $path = resource_path('admin/views'); } else { $path = resource_path('front/views'); } view()->addLocation($path); }

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


Make sure to subscribe to my newsletter and be the first to know about my new post.

Subscribe on Youtube

I post tutorials about various technologies on the youtube channel

Subscribe Now

Make sure to subscribe to my newsletter and be the first to know about my new post.

© 2021