Laravel Separate Views for Admin or Backend and Frontend

This article is about to set up the different scope, Admin or Front, to serve distinctive Route, Controller and view files. This can make a Laravel application more organized.

First, we will see how can we arrange different views for two different sides, admin and front, of the Laravel application.

Let’s start coding 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'

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); }

Now, if you use the code view(‘welcome’) anywhere in the application then it will result try to find file welcome.blade.php in the folder /resources/front/welcome.blade.php

Step 3: Move all the files from resources/views to resources/admin/views and try to visit your homepage, this should work fine.

Step 4: Open routes/web.php file and add the following route to it.

Route::get('admin/login', function () { return view('login'); });

Step 5: Create a file login.blade.php under the directory resources/admin

Step 6: Create a layout file under resources/admin/layouts/admin_app.blade.php, remember this file will be called in any new template we are going to use in the admin to call the header and the footer.

Please watch my video: link here for thorough instructions or download source code directly from Github.


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