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代码
- 2a(
- 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:做项目用框架很方便,但是框架用的久了,就容易把基本的技术给忘掉了。还是要打好基础呀,这样不管用什么框架都不至于用得云里雾里的。
完
来源: http://www.cnblogs.com/yangtoude/p/use-yii2-to-achieve-post-requests.html