Ho realizzato un semplice widget nel mio progetto corrente di yii2. Semplicemente, crea un elenco di opzioni selezionate per tutti i temi di jui e consente all'utente di modificare il tema e salvarlo tramite i cookie.Come creare un widget riutilizzabile in Yii2
Questo widget richiede due file javascript, - sono registrati in run() - uno di questi è il plug-in jquery dei cookie. Chiedo informazioni sull'essere in grado di salvare l'integrità di questo widget e dei suoi file js per renderlo facile da riutilizzare in altri progetti Yii2 senza la necessità, maunalmente, di copiare tutti i file js necessari?
<?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";
}
}
In questo caso penso che dovresti semplicemente pubblicarlo su GitHub/Bitbucket come estensione. – arogachev
@arogachev Non potrebbe essere eseguito localmente? C'è un modo per far sì che il compositore lo produca localmente? – SaidbakR
Forse Satis è un'opzione https://github.com/composer/satis. – arogachev