Yii2

2015-01-25 13 views
8

में एक पुन: प्रयोज्य विजेट कैसे बनाएं I की वर्तमान प्रोजेक्ट में मैंने एक साधारण विजेट बनाया है। बस, यह सभी जुई विषयों के लिए एक चयन विकल्प सूची बनाता है और उपयोगकर्ता को थीम बदलने और कुकीज़ के माध्यम से इसे सहेजने की अनुमति देता है।Yii2

इस विजेट को दो जावास्क्रिप्ट फ़ाइलों की आवश्यकता है, - वे रन() में पंजीकृत हैं - उनमें से एक jquery कुकीज़ प्लगइन है। मैं इस विजेट और इसकी जेएस फाइलों की अखंडता को बचाने के तरीके के बारे में पूछता हूं ताकि अन्य वाईआई 2 परियोजनाओं में बिना किसी आवश्यकता के, फिर से, सभी जरूरी जेएस फाइलों की प्रतिलिपि बनाने के लिए इसे आसान बनाया जा सके?

<?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"; 
    } 
} 
+0

इस मामले में मुझे लगता है कि आपको इसे गिटहब/बिटबकेट पर विस्तार के रूप में प्रकाशित करना चाहिए। – arogachev

+0

@arogachev यह स्थानीय रूप से नहीं किया जा सका? क्या संगीतकार इसे स्थानीय रूप से उत्पादित करने का कोई तरीका है? – SaidbakR

+1

शायद सैटिस एक विकल्प है https://github.com/composer/satis। – arogachev

उत्तर

11

अंततः मुझे समाधान मिला है। यह Yii2 Extensions और AssetBundles पर निर्भर करता है। कहानी सरल है, बस सभी फ़ाइलों को एक फ़ोल्डर में डिफ़ॉल्ट रूप से वाईआई 2 फ़ोल्डरों में रखा गया है, उदाहरण के लिए: सामान्य, विक्रेता.- वैसे, विक्रेता मूल और उन्नत yii2 एप्लिकेशन के टेम्पलेट दोनों में पाया जाता है।

सभी फ़ाइलों के अलावा, i12e मेरे मामले, विजेट क्लास PHP फ़ाइल और जावास्क्रिप्ट फ़ाइलों के लिए, आपको YourWidgetNameAsset php क्लास फ़ाइल बनाना है। वास्तव में, समाधान की मास्टर कुंजी उस वर्ग में निहित है।

मेरे मामले

मैं एक विजेट JuiThemeSelectWidget मैं इसे saidbakrvendor तहत निर्देशिका नाम का एक फ़ोल्डर के अंदर रखा नामित किया है तो हम vendor\saidbakr नाम स्थान है। यही कारण है कि फ़ोल्डर में निम्न चार फ़ाइलें हैं:

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

फ़ाइल नंबर 3 पर निर्भर करता है अंतिम उपयोगकर्ता की पसंद को बचाने के लिए कुकीज़ बनाने के लिए फ़ाइल नंबर 4।

<?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. [email protected] 
* 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', 
    ]; 
} 

यहाँ हम this official source में वर्णित के समान विजेट कुछ के लिए AssetBundle परिभाषित:

अब हम फ़ाइल नंबर 2 JuiThemeSelectAsset.php का कोड देख सकते हैं।

अब हम विजेट वर्ग ही है और इसकी run() विधि के शीर्षक पर एक नज़र ले जाएगा:

<?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 \n $this->script"; 
    } 
} 

यह स्पष्ट है कि हम संपत्ति बंडल this link में वर्णित के रूप में इस्तेमाल किया, लेकिन यहाँ हम बजाय $this->getView() इस्तेमाल किया $this क्योंकि विधि किसी दृश्य से नहीं आती है।

मैं फ़ोल्डर saidbakr नामित संकुचित और this location पर अपलोड या इस GitHub Repository चेकआउट, मैं क्या कर दिया जो इसके नाम Yii2 एक्सटेंशन है की जाँच करने के है।संग्रह फ़ोल्डर की सामग्री को सीधे विक्रेता फ़ोल्डर, के तहत संग्रहित फ़ोल्डर में निकालें, इसलिए फ़ाइल संरचना 'विक्रेता \ saidbakr (ऊपर दी गई सूची में दी गई चार फाइलें) होनी चाहिए, और अपने विचारों में विजेट को निम्न जैसा कुछ उपयोग करना चाहिए :

<?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

yii2 में विजेट जोड़ें घटकों फ़ोल्डर बनाएँ अंदर जड़ directory.then php फ़ाइल बनाएँ। उस फ़ाइल में नामस्थान घटक का उपयोग करें (नेमस्पेस ऐप \ घटक)। विजेट शामिल करें (ऐप \ बेस \ विजेट का उपयोग करें)। कक्षा वर्ग नामस्थान ऐप \ घटक विस्तारित कक्षा बनाएं; वाईआई \ बेस \ विजेट का उपयोग करें; एक दृश्य फ़ोल्डर बनाएं जिसमें विजेट से व्यू फ़ाइल कॉलिंग हो।

 संबंधित मुद्दे