Skip to content

Latest commit

 

History

History
106 lines (82 loc) · 2.36 KB

configuration.md

File metadata and controls

106 lines (82 loc) · 2.36 KB

Views - Installation and Configuration

The spiral/views component is available in Web bundle by default, to install it in alternative builds:

$ composer require spiral/views

Please note that the spiral/framework >= 2.6 already includes this component.

To activate the component use bootloader Spiral\Bootloaders\Views\ViewsBootloader:

[
    // ...
    Spiral\Bootloaders\Views\ViewsBootloader::class,
    // ...
]

The component can work with multiple rendering engines (Twig, Stempler or native PHP templates) and store view files in multiple namespaces.

Configuration

To change the default component configuration create and edit file app/config/views.php:

<?php
declare(strict_types=1);

use Spiral\Views\Engine\Native\NativeEngine;

return [
    'cache'        => [
        'enabled'   => !env('DEBUG', false),
        'directory' => directory('cache') . 'views'
    ],
    'namespaces'   => [
        'default' => [
            directory('views')
        ]
    ],
    'dependencies' => [],
    'engines'      => [
        NativeEngine::class
    ]
];

Auto-Configuration

You can alter some of the component settings using bootloader Spiral\Bootloaders\Views\ViewsBootloader.

View Namespace

To assign a new directory to view namespace:

namespace App\Bootloader;

use Spiral\Boot\Bootloader\Bootloader;
use Spiral\Bootloader\Views\ViewsBootloader;

class AppBootloader extends Bootloader
{
    public function boot(ViewsBootloader $views)
    {
        $views->addDirectory('default', directory('root') . 'views');
    }
}

View Engine

To register a new view engine make sure to implement Spiral\Views\EngineInterface:

namespace App\Bootloader;

use Spiral\Boot\Bootloader\Bootloader;
use Spiral\Bootloader\Views\ViewsBootloader;

class AppBootloader extends Bootloader
{
    public function boot(ViewsBootloader $views)
    {
        $views->addEngine(MyEngine::class);
    }
}

Caching

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.

Purge

Run the console command views:reset to delete view cache:

$ php app.php views:reset

Warmup

To warmup cache before run views:compile or configure:

$ php app.php views:compile

Always warmup view cache before switching worker pool under load.