yidas/codeigniter-widget 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

yidas/codeigniter-widget

Composer 安装命令:

composer require yidas/codeigniter-widget

包简介

Developing

README 文档

README

CodeIgniter Widget


CodeIgniter 3 Widget for reusable building view blocks

Latest Stable Version Latest Unstable Version License

This Widget extension is collected into yidas/codeigniter-pack which is a complete solution for Codeigniter framework.

Features

  • Common interface with Yii2 pattern like

  • Reusable building blocks implementation

  • PSR-4 Namespace support for static call

OUTLINE

DEMONSTRATION

Define a widget then use it in Codeigniter's view:

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

REQUIREMENTS

This library requires the following:

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

INSTALLATION

Run Composer in your Codeigniter project under the folder \application:

composer require yidas/codeigniter-widget

Check Codeigniter application/config/config.php:

$config['composer_autoload'] = TRUE;

You could customize the vendor path into $config['composer_autoload']

CONFIGURATION

Widget extension require yidas/codeigniter-psr4-autoload to implement PSR-4 Namespace, which you need to configure at first:

1. Enabling Hooks

The hooks feature can be globally enabled/disabled by setting the following item in the application/config/config.php file:

$config['enable_hooks'] = TRUE;

2. Adding a Hook

Hooks are defined in the application/config/hooks.php file, add above hook into it:

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

After the configuration, you are able to create widgets.

CREATE WIDGETS

To create a widget, extend from yidas\Widget and override the init() and/or run() methods, remember to defind this widget a current namespace refering by yidas/codeigniter-psr4-autoload.

init() contains the code that initializes the widget properties.

run() contains the code that generates the rendering result of the widget.

In the following example, Hello widget display the partial view with assigning to its message property. If the property is not set, it will display your Codeigniter base_url by default. This widget file should place in 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 provides the PSR-4 Namespace ability for Codeigniter framework.

Rendering View

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

By default, views for a widget should be stored in files in the WidgetPath/views directory, where WidgetPath stands for the directory containing the widget class file.

Therefore, the above example will render the view file app/widgets/views/hello.php, assuming the widget class is located under app/widgets.

You may override the yidas\Widget::getViewPath() method to customize the directory containing the widget view files.

Example:

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

Utilizing CodeIgniter Resources

Widget already prepared $CI property which is CodeIgniter Resources object, you could access it by $this->CI in your widget's init() or run() methods.

    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 on View

Statically call widget() with config in view, and the widget would render into that place:

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

REFERENCE

yidas/codeigniter-widget 适用场景与选型建议

yidas/codeigniter-widget 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 3.13k 次下载、GitHub Stars 达 4, 最近一次更新时间为 2018 年 09 月 20 日, 在 PHP 生态内属于活跃度较高的组件。

它主要适用于以下技术方向: 「codeigniter」 「widget」 「base widget」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。

我们在过去多个企业项目中使用过 yidas/codeigniter-widget 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 yidas/codeigniter-widget 我们能提供哪些服务?
定制开发 / 二次开发

基于 yidas/codeigniter-widget 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 3.13k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 4
  • 点击次数: 0
  • 依赖项目数: 1
  • 推荐数: 0

GitHub 信息

  • Stars: 4
  • Watchers: 2
  • Forks: 8
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2018-09-20