The spiral/views
component is available in Web bundle by default, to install it in alternative builds:
composer require spiral/views
To activate the component, use the bootloader Spiral\Views\Bootloader\ViewsBootloader
:
public function defineBootloaders(): array
{
return [
// ...
\Spiral\Views\Bootloader\ViewsBootloader::class,
// ...
];
}
Read more about bootloaders in the Framework — Bootloaders section.
The component can work with multiple rendering engines (Twig, Stempler or native PHP templates) and store view files in multiple namespaces.
To change the default component configuration, create and edit the file app/config/views.php
:
use Spiral\Views\Engine\Native\NativeEngine;
return [
'cache' => [
'enabled' => !env('DEBUG', false),
'directory' => directory('cache') . 'views'
],
'namespaces' => [
'default' => [
directory('views')
]
],
'dependencies' => [],
'engines' => [
NativeEngine::class
],
'globalVariables' => [
'some_var' => env('SOME_VALUE')
]
];
By default, view caching is turned off if the env variable DEBUG
is set to true. The cache files are stored in
runtime/cache/views
.
Run the console command views:reset
to delete the view cache:
php app.php views:reset
To warmup cache, run views:compile
or configure
:
php app.php views:compile
Warning
Always warmup the view cache before switching the worker pool under load.