douken0530 2015-01-25 03:09
浏览 93
已采纳

如何在Yii2中创建可重用的小部件

I have made a simple widget in my current project of yii2. Simply, it creates a select options list for all jui themes and allow the user to change the theme and save it by the mean of cookies.

This widget need two javascript files, -they are registered in run()- one of them is the jquery cookies plugin. I ask about the being of way to save integrity of this widget and its js files to make it easy to be reused in other Yii2 projects without the need for, maunally, copying all the needed js files?

<?php
namespace common\libs;

use yii;
use yii\base\Widget;
use yii\web\View;
use yii\web\JqueryAsset;
/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of JuiThemeSelectWidget
 *
 * @author Said Bakr
 */
class JuiThemeSelectWidget extends Widget
{
  private $list;
  private $script;
  private static $juiThemeSelectId = 'JuiThemesList';
  public $themeListId;
  public $label;
  public function init() {
    parent::init();
    if ($this->themeListId)   self::$juiThemeSelectId = $this->themeListId;
    $this->list = $this->createSelectList($this->getThemesList());
    $this->makeScript();
  }
  public static function getThemesList()
  {
    $themesPath =  dirname(Yii::$app->basePath).DIRECTORY_SEPARATOR."vendor".DIRECTORY_SEPARATOR."bower".DIRECTORY_SEPARATOR."jquery-ui".DIRECTORY_SEPARATOR."themes";
    $output = [];
    foreach (scandir($themesPath) as $item){
      if (is_dir($themesPath.DIRECTORY_SEPARATOR.$item) && ($item != '.' && $item !='..')) $output[] = $item;
    }
    return $output;
  }

  public static function createSelectList($items)
  { 
    $juiThemeSelectId = self::$juiThemeSelectId;    
    $output = '';
    $output .= "<select id=\"$juiThemeSelectId\">"."
";
    foreach ($items as $item){
      $output .= "<option value='$item'>$item</option>
";
    }
    $output .= "</select>
";
    return $output;
  }

  /**
   * Making the client-side script for the list   */

  private  function makeScript()
  {

    $t = self::$juiThemeSelectId;
    $this->script = <<<EOD

<script>
    var juiThemeSelectId = "$t"   
</script>           
EOD;

  }
  public function run() {
    parent::run();
    $this->getView()->registerJsFile('/myjs/jquery.cookie.js', ['depends' => [JqueryAsset::className()]]);
    $this->getView()->registerJsFile('/myjs/JuiThemeSelect.js', ['depends' => [JqueryAsset::className()]]);
    return "$this->label $this->list 
 $this->script";
  }
}
  • 写回答

2条回答 默认 最新

  • dragon188199 2015-01-25 23:21
    关注

    Finally I have found the solution. It depends on Yii2 Extensions and AssetBundles. The story is simple, just make all files in one folder placed in one of default Yii2 folders, for example: common, vendor.-By the way, vendor is found in both basic and advanced yii2 application's template-.

    In addition to all files, i.e for my case, the widget class php file and the javascripts files, you have to create YourWidgetNameAsset php class file. Indeed, the master key of the solution lies in that class.

    My case

    I have a widget named JuiThemeSelectWidget I placed it inside a folder named saidbakr under vendor directory so we have vendor\saidbakr namespace. That folder contains the following four files:

    1. JuiThemeSelectWidget.php
    2. JuiThemeSelectAsset.php
    3. JuiThemeSelect.js
    4. jquery.cookie.js

    The file number 3 depends on the file number 4 for creating cookies to save the last user's choice.

    Now lets we see the code of file number 2 JuiThemeSelectAsset.php:

    <?php
    namespace vendor\saidbakr;
    use yii\web\AssetBundle;
    
    /*
     * It is free for use and modify with one simple rule:
     * regarding credits for the author either it modified or not
     * Author: Said Bakr. said_fox@yahoo.com
     * http://2index.net
     */
    
    /**
     * Description of Kabb
     *
     * @author Said
     */
    class JuiThemeSelectAsset extends AssetBundle
    {
      public $sourcePath = '@vendor/saidbakr';
    
        public $autoGenerate = true;
        /**
         * @inheritdoc
         */
        public $js = ['jquery.cookie.js','JuiThemeSelect.js'];
        public $depends = [
            'yii\jui\JuiAsset',
        ];
    }
    

    Here we defined AssetBundle for the widget something similar to described in this official source.

    Now we will take a look at the header of the widget class itself and its run() method:

    <?php
    namespace vendor\saidbakr;
    
    use yii;
    use yii\base\Widget;
    //use yii\web\View;
    //use yii\web\JqueryAsset;
    class JuiThemeSelectWidget extends Widget
    {
      // ...... Class code....
    
    public function run() {
        parent::run();
        JuiThemeSelectAsset::register($this->getView());    
        return "$this->label $this->list 
     $this->script";
      }
    }
    

    It is clear that we used the asset bundle as described in this link but here we used $this->getView() instead of $this because the method does not invoked from a view.

    I have compressed the folder named saidbakr and uploaded it to this location or checkout this GitHub Repository , to check what have I made which its name is Yii2 Extension. Just extract the contents of the archive to folder named saidbakr directly under vendor folder, So the file structure must be `vendor\saidbakr(the four files regarded in the list above), and use the widget in your views something like the following:

    <?php
    use yii\helpers\Html;
    use yii\widgets\ActiveForm;
    use yii\jui\DatePicker;
    use vendor\saidbakr\JuiThemeSelectWidget;
    ?>
    <div>
    <?= JuiThemeSelectWidget::widget(['label' => 'Select New JUI Theme', 'themeListId' => 'fox']) ;?>
    <div class="profile-form">
    </div> 
    <h2>Testing Elements for the JUI</h2>
    <form>
    <select id="sel">
     <option value="1">One</option>
     <option value="2">Two</option>
     <option value="3">Three</option>
    </select>
    </form>
    <?php $this->registerJs("$('#sel').selectmenu();") ;?>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 微带串馈天线阵列每个阵元宽度计算
  • ¥15 关于无人驾驶的航向角
  • ¥15 keil的map文件中Image component sizes各项意思
  • ¥30 BC260Y用MQTT向阿里云发布主题消息一直错误
  • ¥20 求个正点原子stm32f407开发版的贪吃蛇游戏
  • ¥15 划分vlan后,链路不通了?
  • ¥20 求各位懂行的人,注册表能不能看到usb使用得具体信息,干了什么,传输了什么数据
  • ¥15 Vue3 大型图片数据拖动排序
  • ¥15 Centos / PETGEM
  • ¥15 划分vlan后不通了