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:
<?php
return [
// Other options ...
'paths' => [
base_path('client_views'),
],
// Other options ...
];
Multiple paths can be provided as well:
<?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.
{{ 'Testing new default directory' }}
<?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