Tôi đã tạo một tiện ích đơn giản trong dự án hiện tại của mình là yii2. Đơn giản, nó tạo ra một danh sách tùy chọn lựa chọn cho tất cả các chủ đề jui và cho phép người dùng thay đổi chủ đề và lưu nó theo ý nghĩa của cookie.Cách tạo tiện ích có thể sử dụng lại trong Yii2
Tiện ích này cần hai tệp javascript, - chúng được đăng ký chạy() - một trong số đó là plugin cookie jquery. Tôi hỏi về cách để tiết kiệm tính toàn vẹn của widget này và các tập tin js của nó để làm cho nó dễ dàng được tái sử dụng trong các dự án Yii2 khác mà không cần, maunally, sao chép tất cả các file js cần thiết?
<?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\">"."\n";
foreach ($items as $item){
$output .= "<option value='$item'>$item</option>\n";
}
$output .= "</select>\n";
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 \n $this->script";
}
}
Trong trường hợp này, tôi nghĩ bạn chỉ nên xuất bản nó trên GitHub/Bitbucket làm phần mở rộng. – arogachev
@arogachev Không thể thực hiện tại địa phương? Có cách nào để làm cho nhà soạn nhạc sản xuất tại địa phương không? – SaidbakR
Có lẽ Satis là một tùy chọn https://github.com/composer/satis. – arogachev