標籤:
因為近期給朋友公司做個門戶網站,把荒置了6、7年的PHP又重新拾起,發現PHP這些年興旺多了,很多新的東西看的不明不白,研究了幾個架構ZendFramework、thinkphp、Symfony、yii2,也曾經考慮直接用網上現有的開源cms,要麼因為商用授權太貴,要麼後台太複雜,要麼覆蓋行業太多導致業務複雜,看了一通代碼,頭暈得很,最終選擇yii2自己開發個輕量的cms+shop系統,慢慢的做,應該能做出後台簡單、有自己特色的網站系統來!一下是我的一些開發過程和經驗,因為沒有考慮太多的技術和通用,所以有局限。網友可參考使用(請註明出處即可,我也使用了別人的成果)
在yii2中把一些js的動態組件封裝成可參數化的Widget對象能夠簡化功能邏輯,代碼複用可大大提高。
一般組件由樣式、js指令碼、html標籤構成,在yii2中被放到AssetBundle和Widget中處理。Asset Bundle這個英文的解釋是把有用的東西捆紮在一塊,對的!它就是把一些公用的css和js捆紮成自訂的包,Widget用來放置客戶化的樣式、js指令碼、html標籤輸出到瀏覽器。
一下是對SuperSlide2中的“焦點圖 / 投影片”(來源http://www.SuperSlide2.com/)的Widget封裝。
首先從AssetBundle繼承出自己的SuperSlideAsset(檔案名稱為SuperSlideAsset.php),把jquery.superslide.2.1.1.js和它使用樣式捆紮,代碼如下:
<?php
namespace yii\widgets;
use yii\web\AssetBundle;
use yii\web\View;
class SuperSlideAsset extends AssetBundle
{
public $sourcePath = ‘@yii/assets/superslide‘;
public $js = [
‘jquery.superslide.2.1.1.js‘,
];
public $css = [
‘themes/default/default.css‘,
];
public $depends = [
‘yii\web\YiiAsset‘,
];
}
檔案jquery.superslide.2.1.1.js和它使用樣式放到這個路徑:
然後從Widget繼承編寫自己的SuperSlideWidget,主要東西有init()初始化參數、註冊js和css檔案、輸出html動態標籤。代碼如下:
<?php
namespace yii\widgets;
use Yii;
use yii\helpers\Html;
use yii\helpers\Json;
use yii\web\JsExpression;
use yii\base\Arrayable;
use yii\i18n\Formatter;
use yii\base\InvalidConfigException;
use yii\base\Model;
use yii\base\Widget;
use yii\helpers\ArrayHelper;
class SuperSlideWidget extends Widget
{
const PLUGIN_NAME = ‘SuperSlide‘;
/**
* SuperSlide Options
* @var array
*/
public $rows;
public $name;
public $width;
public $height;
public $themeType;//default
public $autoPlay;//true,false
//[v1.0] fade:漸顯; || top:上滾動;|| left:左滾動;|| topLoop:上迴圈滾動;|| leftLoop:左迴圈滾動;|| topMarquee:上無縫迴圈滾動;|| leftMarquee:左無縫迴圈滾動;
//[v2.0] fold:淡入淡出;[v2.1] slideDown:下拉效果
public $effect;//動畫效果
public $trigger;//"mouseover" titCell觸發方式 || mouseover:滑鼠移過觸發;|| click:滑鼠點擊觸發;
public $easing;//"swing" 緩動效果;[v2.1]更改預設效果為“swing”,使效果更流暢
public $delayTime;//毫秒;轉場效果期間(一次轉場效果執行所用的時間長度)。
public $mouseOverStop;//true 滑鼠移到容器層(無縫滾動是mainCell)是否停止播放
public $pnLoop; //true 前/後按鈕是否繼續迴圈,若為false則當翻動到最前/後頁時,前/後按鈕點擊無效,同時增加prevStop/nextStop類名控制樣色
/**
* Initializes the widget. init()初始化參數、
* If you override this method, make sure you call the parent implementation first.
*/
public function init()
{
if ($this->name === null) {
throw new InvalidConfigException("‘name‘ properties must be specified.");
}
if ($this->rows === null) {
throw new InvalidConfigException("‘rows‘ properties must be specified.");
}
if ($this->width === null) {
$this->width="1024px";
}
if ($this->height === null) {
$this->height="400px";
}
if ($this->autoPlay === null) {
$this->autoPlay="true";
}
if ($this->effect === null) {
$this->effect="fade";
}
if ($this->trigger === null) {
$this->trigger="mouseover";
}
if ($this->easing === null) {
$this->easing="swing";
}
if ($this->delayTime === null) {
$this->delayTime="500";
}
if ($this->mouseOverStop === null) {
$this->mouseOverStop="true";
}
if ($this->pnLoop === null) {
$this->pnLoop="true";
}
if ($this->themeType === null) {
$this->themeType="default";
}
parent::init();
}
/**
* @inheritdoc,
titCell ".hd li" 導航元素對象(滑鼠的觸發元素對象) 圖解
mainCell ".bd" 切換元素的包裹層對象
*/
public function run()
{
$this->registerClientScript();//註冊js和css檔案
//輸出html動態標籤
$out= "<div id=‘".$this->name."‘ class=‘slideBox‘ width=‘".$this->width."‘ height=‘".$this->height."‘ >\n";
$out=$out. "<div class=‘hd‘>\n";
$iRow=1;
$hdul=" <ul style=‘overflow:hidden; zoom:1; float:left;margin:0; padding:0; list-style-type:none;‘>\n";
while ($iRow<count($this->rows)+1 )
{
$hdul=$hdul."<li>".$iRow."</li>";
$iRow=$iRow+1;
}
$hdul=$hdul."</ul>\n";
$out=$out. $hdul;
$out=$out. "</div>\n";
$out=$out. "<div class=‘bd‘>\n";
$out=$out. "<ul style=‘margin:0; padding:0;list-style-type:none;‘ >\n";
foreach ($this->rows as $k => $ad ) {
$out=$out."<li><a href=‘".$ad[‘link_url‘]."‘ target=‘".$ad[‘target‘]."‘>";
$out=$out." <img src=‘".$ad[‘image_url‘]."‘ width=‘100%‘ height=‘".$this->height."‘ /></a></li>\n";
}
$out=$out. " </ul>\n";
$out=$out. "</div>\n";
$out=$out. "<!-- 下面是前/後按鈕代碼,如果不需要刪除即可 -->\n";
$out=$out. "<a class=‘prev‘ href=‘javascript:void(0)‘></a>\n";
$out=$out. "<a class=‘next‘ href=‘javascript:void(0)‘></a>\n";
$out=$out. "</div>\n";
echo $out;
}
/**
* register client scripts(css, javascript)
*/
public function registerClientScript()
{
$view = $this->getView();
$asset = SuperSlideAsset::register($view); //這裡使用SuperSlideAsset
if ($this->themeType != ‘default‘) {
$view->registerCssFile($asset->baseUrl . "/themes/{$this->themeType}/{$this->themeType}.css", [‘depends‘ => ‘yii\widgets\SuperSlideAsset‘]);
}
$options = [];
$options[‘mainCell‘] = ‘.bd ul‘;
$options[‘autoPlay‘] = $this->autoPlay;
$options[‘effect‘] = $this->effect;
$options[‘trigger‘] = $this->trigger;
$options[‘easing‘] = $this->easing;
$options[‘delayTime‘] = $this->delayTime;
$options[‘mouseOverStop‘] = $this->mouseOverStop;
$options[‘pnLoop‘] = $this->pnLoop;
$js=" jQuery(‘.slideBox‘).slide(".Json::encode($options)."); \n";
$view->registerJs($js,3);//View::POS_END
}
}
使用SuperSlideWidget的例子:
$rows=Yii::$app->db->createCommand($sql)->query();//需要包含link_url,image_url,target欄位
echo SuperSlideWidget::widget([
‘name‘ => ‘slide_windows‘,
‘width‘ => ‘1024px‘,
‘rows‘=> $rows,
‘height‘ => ‘350px‘,
‘themeType‘ => ‘default‘,
])
至此,大功告成!
PHP在yii2中封裝SuperSlide 投影片編寫自己的SuperSlideWidget的例子