yidas/codeigniter-widget

1.0.0 2018-09-23 08:09 UTC

This package is auto-updated.

Last update: 2024-08-29 04:25:48 UTC


README

CodeIgniter Widget


CodeIgniter 3 Widget 可重用视图构建块扩展

Latest Stable Version Latest Unstable Version License

此 Widget 扩展已收集到 yidas/codeigniter-pack,这是一个针对 Codeigniter 框架的完整解决方案。

功能

  • 与 Yii2 模式类似的通用接口

  • 可重用构建块实现

  • PSR-4 命名空间 支持

概览

DEMONSTRATION

定义一个 Widget 然后在 Codeigniter 的视图中使用它

<?php
use app\widgets\Hello;
?>
<div>
<?=Hello::widget(['message' => 'Good morning']);?>
</div>

REQUIREMENTS

此库需要以下内容

  • PHP 5.4.0+
  • CodeIgniter 3.0.0+
  • yidas/codeigniter-psr4-autoload 1.0.0+

INSTALLATION

\application 文件夹下运行 Composer 命令

composer require yidas/codeigniter-widget

检查 Codeigniter application/config/config.php 文件

$config['composer_autoload'] = TRUE;

您可以在 $config['composer_autoload'] 中自定义供应商路径

CONFIGURATION

Widget 扩展需要 yidas/codeigniter-psr4-autoload 来实现 PSR-4 命名空间,您需要首先进行配置

1. 启用钩子

可以通过在 application/config/config.php 文件中设置以下项来全局启用/禁用钩子功能。

$config['enable_hooks'] = TRUE;

2. 添加钩子

钩子在 application/config/hooks.php 文件中定义,将上述钩子添加到其中。

$hook['pre_system'][] = [new yidas\Psr4Autoload, 'register'];

配置完成后,您可以创建 Widget。

CREATE WIDGETS

要创建一个 Widget,从 yidas\Widget 继承并重写 init() 和/或 run() 方法,请记住使用 yidas/codeigniter-psr4-autoload 定义此 Widget 的当前命名空间。

init() 包含初始化 Widget 属性的代码。

run() 包含生成 Widget 渲染结果的代码。

在下面的示例中,Hello Widget 显示与分配给其 message 属性的局部视图。如果未设置属性,则默认显示您的 Codeigniter base_url。此 Widget 文件应放在 application/widgets/Hello.php

<?php

namespace app\widgets;

use yidas\Widget;

class Test extends Widget
{
    // Customized variable for Widget
    public $message;
    
    public function init()
    {
        // Use $this->CI for accessing Codeigniter object
        $baseUrl = $this->CI->config->item('base_url');
        
        if ($this->message === null) {
            $this->message = "Your Site: {$baseUrl}";
        }
    }
    
    public function run()
    {
        // Render the view `test.php` in `WidgetPath/views` directory,
        return $this->render('test', [
            'message' => $this->message,
            ]);
    }
}

yidas/codeigniter-psr4-autoload 为 Codeigniter 框架提供了 PSR-4 命名空间功能。

渲染视图

public string render(string $view, array $variables=[])

默认情况下,Widget 的视图应存储在 WidgetPath/views 目录中的文件中,其中 WidgetPath 代表包含 Widget 类文件的目录。

因此,上述示例将渲染视图文件 app/widgets/views/hello.php,假设 Widget 类位于 app/widgets 下。

您可以重写 yidas\Widget::getViewPath() 方法来自定义包含 Widget 视图文件的目录。

示例

    public function run()
    {
        return $this->render('view_name', [
            'variable' => $this->property,
            ]);
    }

利用 CodeIgniter 资源

Widget 已经准备了 $CI 属性,它是 CodeIgniter 资源对象,您可以通过 $this->CI 在 Widget 的 init()run() 方法中访问它。

    public function run()
    {
        // Get data from a model
        $this->CI->load->model('Menu_model');
        $list = $this->CI->Menu_model->getList();
        
        // Build widget's view with list data
        return $this->render('test', [
            'list' => $list,
            ]);
    }

USAGE

视图上的 Widget

在视图中静态调用 widget() 并传入配置,组件会渲染到该位置

<?php
use app\widgets\Hello;
?>
<div>
<?=Hello::widget(['message' => 'Good morning']);?>
</div>

参考