yii2提供了很多幫助類,比如Html、Url、Json等,可以很方便的實現一些功能,下面簡單說下這個Html。用yii2寫view時時經常會用到它,今天在改寫一個頁面時又用到了它。它比較好用的地方就在於,它不僅僅是生成一個簡單的html標簽,結合yii2自己的靜態資源文件yii.js可以很方便的實現一個post請求。
yii2將這些功能都做好了封裝,只要在合適的地方調用它的方法就可以了,可以說yii2是個可以開箱即用的框架,你可以用它很快的實現一個需要的功能:比如在頁面中放置一個刪除按鈕,點擊按鈕發送post請求,彈出確認對話框。如果沒有yii\helpers\Html類和yii.js,那么你需要寫大量的js/jquery來實現這個功能。如果用yii2的話,下面的代碼就可以實現:
1 // html代碼 2 <?= Html::a( 3 '刪除', 4 [ 5 'delete', 6 'id' => $id, 7 ], 8 [ 9 'data' => [ 10 'confirm' => '你確定要刪除嗎?', 11 'method' => 'post', 12 ], 13 ] 14 ) 15 ?> 16 // html代碼
它會在頁面中生成下面一段html代碼:
<a href="delete?id=1" data-confirm="你確定要退出嗎?" data-method="post">刪除</a>
點擊這個按鈕會彈出對話框,確認刪除后會發送post請求。那么這個post請求是如何發送的呢?到現在為止可是一段js代碼都沒寫呢。
yii2框架隱藏了技術實現的細節,post請求的實現在yii.js中。在yii.js中,定義了window.yii對象,並初始化了window.yii對象的initModule方法:
window.yii = (function ($) {
var pub = {
// 定義了處理事件的方法,比如下面這個:
confirm: function (message, ok, cancel) {
if (window.confirm(message)) {
!ok || ok();
} else {
!cancel || cancel();
}
},
handleAction: function ($e, event) {
var $form = $e.attr('data-form') ? $('#' + $e.attr('data-form')) : $e.closest('form'),
method = !$e.data('method') && $form ? $form.attr('method') : $e.data('method'),
// 其他省略
},
// 其他省略
};
// 初始化模塊
initModule: function (module) {
if (module.isActive !== undefined && !module.isActive) {
return;
}
if ($.isFunction(module.init)) {
module.init();
}
$.each(module, function () {
if ($.isPlainObject(this)) {
pub.initModule(this);
}
});
},
// 初始化方法
init: function () {
initCsrfHandler();
initRedirectHandler();
initAssetFilters();
initDataMethods();
},
return pub;
})(window.jQuery);
window.jQuery(function () {
window.yii.initModule(window.yii);
});
其中上面的initDataMethods()會調用pub.handleAction方法:
function initDataMethods() {
var handler = function (event) {
var $this = $(this),
method = $this.data('method'),
message = $this.data('confirm'),
form = $this.data('form');
if (method === undefined && message === undefined && form === undefined) {
return true;
}
if (message !== undefined) {
$.proxy(pub.confirm, this)(message, function () {
pub.handleAction($this, event);
});
} else {
pub.handleAction($this, event);
}
event.stopImmediatePropagation();
return false;
};
// handle data-confirm and data-method for clickable and changeable elements
$(document).on('click.yii', pub.clickableSelector, handler)
.on('change.yii', pub.changeableSelector, handler);
}
可以看到這個方法會獲取上面生成的a標簽的data屬性值,然后交給handlerAction來處理。handlerAction通過動態生成一個form來處理各種請求,最后通過觸發submit事件來提交。
// 其他省略 $form = $('<form/>', {method: method, action: action}); var target = $e.attr('target'); if (target) { $form.attr('target', target); } if (!/(get|post)/i.test(method)) { $form.append($('<input/>', {name: '_method', value: method, type: 'hidden'})); method = 'post'; $form.attr('method', method); } if (/post/i.test(method)) { var csrfParam = pub.getCsrfParam(); if (csrfParam) { $form.append($('<input/>', {name: csrfParam, value: pub.getCsrfToken(), type: 'hidden'})); } } $form.hide().appendTo('body'); // 其他省略
PS:做項目用框架很方便,但是框架用的久了,就容易把基本的技術給忘掉了。還是要打好基礎呀,這樣不管用什么框架都不至於用得雲里霧里的。
完