Set Custom Path for Blade Templates in Laravel 9

Set Custom Path for Blade Templates in Laravel 9

By default, in Laravel application Blade templates are stored in the resources/views directory. There might be required to store the Blade templates in different location. This tutorial shows how to set custom path for Blade templates in Laravel 9 application.

Custom path can be set using the paths option in the config/views.php configuration file:

config/view.php

<?php

return [
    // Other options ...

    'paths' => [
        base_path('client_views'),
    ],

    // Other options ...
];

Multiple paths can be provided as well:

config/view.php

<?php

return [
    // Other options ...

    'paths' => [
        base_path('client_views'),
        resource_path('views'),
    ],

    // Other options ...
];

Order is important when multiple template paths are specified. In our case, if the Blade template is not found in the client_views directory, then the template will be loaded from the resources/views directory.

Don't forget to clear configuration cache using command:

php artisan config:cache

To test, create the controller and template in a new directory.

client_views/test/index.blade.php

{{ 'Testing new default directory' }}

app/Http/Controllers/TestController.php

<?php

namespace App\Http\Controllers;

use Illuminate\View\View;

class TestController extends Controller
{
    public function index(): View
    {
        return view('test.index');
    }
}

Leave a Comment

Cancel reply

Your email address will not be published.