跳至内容

集成自定义视图库

如果您的应用程序使用不同于 CodeIgniter 内置 view() 帮助程序的方法将视图文件转换为 HTML,则可以在 Shield 中呈现视图的任何位置轻松集成您的系统。

所有控制器和操作都使用 CodeIgniter\Shield\Traits\Viewable 特性,该特性提供了一个简单的 view() 方法,它采用与 view() 帮助程序相同的参数。这允许您扩展操作或控制器,并且仅更改呈现视图的单个方法,从而使所有逻辑保持不变,因此您的应用程序在不需要更改时无需维护 Shield 逻辑。

<?php

declare(strict_types=1);

namespace App\Controllers;

use Acme\Themes\Traits\Themeable;
use CodeIgniter\Shield\Controllers\LoginController;

class MyLoginController extends LoginController
{
    use Themable;

    protected function view(string $view, array $data = [], array $options = []): string
    {
        return $this->themedView($view, $data, $options);
    }
}