yii2提供了很多幫助類,比如Html、Url、Json等,可以很方便的實現一些功能,下面簡單說下這個Html。用yii2寫view時時經常會用到它,今天在改寫一個頁面時又用到了它。它比較好用的地方就在于,它不僅僅是生成一個簡單的html標簽,結合yii2自己的靜態資源文件yii.js可以很方便的實現一個post請求。
yii2將這些功能都做好了封裝,只要在合適的地方調用它的方法就可以了,可以說yii2是個可以開箱即用的框架,你可以用它很快的實現一個需要的功能:比如在頁面中放置一個刪除按鈕,點擊按鈕發送post請求,彈出確認對話框。如果沒有yii/helpers/Html類和yii.js,那么你需要寫大量的js/jquery來實現這個功能。如果用yii2的話,下面的代碼就可以實現:
- <?= Html::a(
- '刪除',
- [
- 'delete',
- 'id' => $id,
- ],
- [
- 'data' => [
- 'confirm' => '你確定要刪除嗎?',
- 'method' => 'post',
- ],
- ]
- )
- ?>
它會在頁面中生成下面一段html代碼:
刪除:
點擊這個按鈕會彈出對話框,確認刪除后會發送post請求。那么這個post請求是如何發送的呢?到現在為止可是一段js代碼都沒寫呢。
yii2框架隱藏了技術實現的細節,post請求的實現在yii.js中。在yii.js中,定義了window.yii對象,并初始化了window.yii對象的initModule方法:
- window.yii = (function($) {
- varpub = {
- // 定義了處理事件的方法,比如下面這個:
- 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();
- },
- returnpub;
- })(window.jQuery);
- window.jQuery(function() {
- window.yii.initModule(window.yii);
- });
其中上面的initDataMethods()會調用pub.handleAction方法:
- functioninitDataMethods() {
- varhandler =function(event) {
- var$this= $(this),
- method = $this.data('method'),
- message = $this.data('confirm'),
- form = $this.data('form');
- if(method === undefined && message === undefined && form === undefined) {
- returntrue;
- }
- if(message !== undefined) {
- $.proxy(pub.confirm,this)(message,function() {
- pub.handleAction($this, event);
- });
- }else{
- pub.handleAction($this, event);
- }
- event.stopImmediatePropagation();
- returnfalse;
- };
- // 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 = $('
- ', {method: method, action: action});
- vartarget = $e.attr('target');
- if(target) {
- $form.attr('target', target);
- }
- if(!/(get|post)/i.test(method)) {
- $form.append($('', {name:'_method', value: method, type:'hidden'}));
- method ='post'
- $form.attr('method', method);
- }
- if(/post/i.test(method)) {
- varcsrfParam = pub.getCsrfParam();
- if(csrfParam) {
- $form.append($('', {name: csrfParam, value: pub.getCsrfToken(), type:'hidden'}));
- }
- }
- $form.hide().appendTo('body');
其他省略:
PS:做項目用框架很方便,但是框架用的久了,就容易把基本的技術給忘掉了。還是要打好基礎呀,這樣不管用什么框架都不至于用得云里霧里的。
新聞熱點
疑難解答