Blogs

Set a different directory for view files in Laravel

Set a different directory for view files in Laravel

In order to set a different location for your view files you will need to create a new ServiceProvider class and add it to the providers array in your app.php (config/app.php) file.

Add the following line to your app.php file

App\Providers\AppServiceProvider::class

Then create a new file in app/Providers called ViewServiceProvider.php

ViewServiceProvider.php
<?php namespace App\Providers;

use Illuminate\Support\Facades\View; 

class ViewServiceProvider extends \Illuminate\View\ViewServiceProvider { 
    
    public function register() { 
        View::addNamespace('frontend', [base_path().'/locationone',base_path().'/locationotwo']);
    }

}

addNameSpace accepts two parameters, one is the name which means instead of calling view('views.frontend.pages.index') you can now call view('frontend::pages.index') (note the ::)

The second parameter is an array of locations, if the file isn't found in the first location it will checked the second, then third, and so on..

So this becomes very handy if you have a theme structure to your application.