今天要给各位朋友分享的是YII2框架中如何使用yii.js实现的post请求例子分享,简单分析下用yii2的yii\helpers\Html类和yii.js实现的post请求的方法,有兴趣的朋友赶紧来了解一下吧。
yii2提供了不少帮助类,例如Html、Url、Json等,能够非常方便的实现一些功能,现在就来简单说一下这个Html。用yii2写view的时候经经常会用到它,今天在改写一个页面的时候又用到了它。它好用的地方就在于,它不只是生成一个简单的html标签,结合yii2自己的静态资源文件yii.js能够非常方便的实现一个post请求。
yii2将这一些功能都做好了封装,只要在合适的地方调用它的方法就行了,可以说yii2是一个能够开箱即用的框架,你能够用它很快的实现一个需要的功能:例如在页面中放置一个删除按钮,点击按钮发送post请求,弹出确认对话框。要是没有yii\helpers\Html类和yii.js,那么你需要写大量的js/jquery来实现这个功能。要是用yii2的话,下面的代码就能够实现:
// html代码
<?= Html::a(
'删除',
[
'delete',
'id' => $id,
],
[
'data' => [
'confirm' => '你确定要删除吗,',
'method' => 'post',
],
]
)
?>
// html代码
它会在页面中生成下面一段html代码:
<a href="delete?id=1" rel="external nofollow" 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');
// 其他省略