Yii2实现增删改查后留在当前页的方法详解

韦小宝
发布: 2023-03-17 16:58:01
原创
1757 人浏览过

实现增删改查操作成功后仍留在当前页,能给用户一个良好的体验。但是 Yii2 框架本身是没有在增删改查操作成功后仍留在当前页的效果的,要实现这样的一个效果得自己写。我的原则是不动核心代码,始终坚持自己的原则,现实现了我把它分享出来。殊途同归,如有更好的实现增删改查的方法,欢迎交流。

封装代码

共有两个文件ActionColumn.php和Helper.php

1、ActionColumn.php文件

<?php

use Closure;
use kartik\icons\Icon;
use Yii;
use yii\grid\Column;
use yii\helpers\ArrayHelper;
use yii\helpers\Html;
use yii\helpers\Url;
use common\components\Helper;

/*
*重写ActionColumn
 */
class ActionColumn extends Column
{ 
 public $buttons;

 private $defaultButtons = [];

 private $callbackButtons;
 
 public $controller;

 public $urlCreator;

 public $url_append = &#39;&#39;;

 public $appendReturnUrl = true; //默认为true,返回当前链接

 public function init()
 {
 parent::init();
 
 $this->defaultButtons = [
  [
  &#39;url&#39; => &#39;view&#39;,
  &#39;icon&#39; => &#39;eye&#39;,
  &#39;class&#39; => &#39;btn btn-success btn-xs&#39;,
  &#39;label&#39; => Yii::t(&#39;yii&#39;, &#39;View&#39;),
  &#39;appendReturnUrl&#39; => false,
  &#39;url_append&#39; => &#39;&#39;,
  &#39;keyParam&#39; => &#39;id&#39;,//是否传id,不传设置null
  ],
  [
  &#39;url&#39; => &#39;update&#39;,
  &#39;icon&#39; => &#39;pencil&#39;,
  &#39;class&#39; => &#39;btn btn-primary btn-xs&#39;,
  &#39;label&#39; => Yii::t(&#39;yii&#39;, &#39;Update&#39;),
  ],
  [
  &#39;url&#39; => &#39;delete&#39;,
  &#39;icon&#39; => &#39;trash-o&#39;,
  &#39;class&#39; => &#39;btn btn-danger btn-xs&#39;,
  &#39;label&#39; => Yii::t(&#39;yii&#39;, &#39;Delete&#39;),
  &#39;options&#39; => [
   &#39;data-action&#39; => &#39;delete&#39;,
  ],
  ]
 ];


 if (null === $this->buttons) {
  $this->buttons = $this->defaultButtons;
 } elseif ($this->buttons instanceof Closure) {
  $this->callbackButtons = $this->buttons;
 }
 }

 
 public function createUrl(
 $action,
 $model,
 $key,
 $index,
 $appendReturnUrl = null,
 $url_append = null,
 $keyParam = &#39;id&#39;,
 $attrs = []
 ) {
 if ($this->urlCreator instanceof Closure) {
  return call_user_func($this->urlCreator, $action, $model, $key, $index);
 } else {
  $params = [];
  if (is_array($key)) {
  $params = $key;
  } else {
  if (is_null($keyParam) === false) {
   $params = [$keyParam => (string)$key];
  }
  }
  $params[0] = $this->controller ? $this->controller . &#39;/&#39; . $action : $action;
  foreach ($attrs as $attrName) {
  if ($attrName === &#39;model&#39;) {
   $params[&#39;model&#39;] = $model;
  } elseif ($attrName === &#39;mainCategory.category_group_id&#39; && $model->getMainCategory()) {
   $params[&#39;category_group_id&#39;] = $model->getMainCategory()->category_group_id;
  } else {
   $params[$attrName] = $model->getAttribute($attrName);
  }
  }
  if (is_null($appendReturnUrl) === true) {
  $appendReturnUrl = $this->appendReturnUrl;
  }
  if (is_null($url_append) === true) {
  $url_append = $this->url_append;
  }
  if ($appendReturnUrl) {
  $params[&#39;returnUrl&#39;] = Helper::getReturnUrl();
  }
  return Url::toRoute($params) . $url_append;
 }
 }


 protected function renderDataCellContent($model, $key, $index)
 { 
 if ($this->callbackButtons instanceof Closure) {
  $btns = call_user_func($this->callbackButtons, $model, $key, $index, $this); 
  if (null === $btns) {
  $this->buttons = $this->defaultButtons;
  } else {
  $this->buttons = $btns;
  }
 }
 $min_width = count($this->buttons) * 34; //34 is button-width
 $data = Html::beginTag(&#39;div&#39;, [&#39;class&#39; => &#39;btn-group&#39;, &#39;style&#39; => &#39;min-width: &#39; . $min_width . &#39;px&#39;]);
 foreach ($this->buttons as $button) {
  $appendReturnUrl = ArrayHelper::getValue($button, &#39;appendReturnUrl&#39;, $this->appendReturnUrl);
  $url_append = ArrayHelper::getValue($button, &#39;url_append&#39;, $this->url_append);
  $keyParam = ArrayHelper::getValue($button, &#39;keyParam&#39;, &#39;id&#39;);
  $attrs = ArrayHelper::getValue($button, &#39;attrs&#39;, []);
  Html::addCssClass($button, &#39;btn&#39;);
  Html::addCssClass($button, &#39;btn-sm&#39;);
  $buttonText = isset($button[&#39;text&#39;]) ? &#39; &#39; . $button[&#39;text&#39;] : &#39;&#39;; 
  $data .= Html::a(
   $button[&#39;label&#39;] . $buttonText,
   $url = $this->createUrl(
   $button[&#39;url&#39;],
   $model,
   $key,
   $index,
   $appendReturnUrl,
   $url_append,
   $keyParam,
   $attrs
   ),
   ArrayHelper::merge(
   isset($button[&#39;options&#39;]) ? $button[&#39;options&#39;] : [],
   [
    //&#39;data-pjax&#39; => 0,
    // &#39;data-action&#39; => $button[&#39;url&#39;],
    &#39;class&#39; => $button[&#39;class&#39;],
    &#39;title&#39; => $button[&#39;label&#39;],
   ]
   )
  ) . &#39; &#39;;
 }
 $data .= &#39;</div>&#39;; 
 return $data;
 }

}
登录后复制

2、Helper.php文件

<?php

use Yii;

class Helper
{ 
 private static $returnUrl;
 public static $returnUrlWithoutHistory = false;

 /**
 * @param int $depth
 * @return string
 */
 public static function getReturnUrl()
 {
 if (is_null(self::$returnUrl)) {
  $url = parse_url(Yii::$app->request->url);
  $returnUrlParams = [];
  if (isset($url[&#39;query&#39;])) {
  $parts = explode(&#39;&&#39;, $url[&#39;query&#39;]);
  foreach ($parts as $part) {
   $pieces = explode(&#39;=&#39;, $part);
   if (static::$returnUrlWithoutHistory && count($pieces) == 2 && $pieces[0] === &#39;returnUrl&#39;) {
   continue;
   }
   if (count($pieces) == 2 && strlen($pieces[1]) > 0) {
   $returnUrlParams[] = $part;
   }
  }
  }
  if (count($returnUrlParams) > 0) {
  self::$returnUrl = $url[&#39;path&#39;] . &#39;?&#39; . implode(&#39;&&#39;, $returnUrlParams);
  } else {
  self::$returnUrl = $url[&#39;path&#39;];
  }
 }
 return self::$returnUrl;
 }
}
登录后复制

视图调用

1、直接调用,把Yii2自带的['class' => 'yiigridActionColumn']换成我们新写的['class' => 'common\components\ActionColumn'] 。

2、如直接调用不能满足你的要求可以自定义链接,自定义链接的写法如下:

[
 &#39;class&#39; => &#39;common\components\ActionColumn&#39;,
 &#39;urlCreator&#39; => function($action, $model, $key, $index) use ($id) {
 //自定义链接传的参数
 $params = [
  $action,
  &#39;option_id&#39; => $model->option_id, 
  &#39;id&#39; => $id,
 ];
 $params[&#39;returnUrl&#39;] = common\components\Helper::getReturnUrl();
 return yii\helpers\Url::toRoute($params);
 },
 &#39;buttons&#39; => [
   [
   &#39;url&#39; =>&#39;view&#39;,
   &#39;class&#39; => &#39;btn btn-success btn-xs&#39;,
   &#39;label&#39; => Yii::t(&#39;yii&#39;, &#39;View&#39;),
   &#39;appendReturnUrl&#39; => false,//是否保留当前URL,默认为true
   &#39;url_append&#39; => &#39;&#39;,
   &#39;keyParam&#39; => &#39;id&#39;, //是否传id,不传设置null
   ],
   [
   &#39;url&#39; => &#39;update&#39;,
   &#39;class&#39; => &#39;btn btn-primary btn-xs btn-sm&#39;,
   &#39;label&#39; => Yii::t(&#39;yii&#39;, &#39;Update&#39;),
   &#39;appendReturnUrl&#39; => true,//是否保留当前URL,默认为true
   &#39;url_append&#39; => &#39;&#39;,
   &#39;keyParam&#39; => &#39;id&#39;, //是否传id,不传设置null
   ],
   [
   &#39;url&#39; => &#39;delete&#39;,
   &#39;class&#39; => &#39;btn btn-danger btn-xs btn-sm&#39;,
   &#39;label&#39; => Yii::t(&#39;yii&#39;, &#39;Delete&#39;),
   &#39;options&#39; => [
   &#39;data-action&#39; => &#39;delete&#39;,
   ],
   &#39;appendReturnUrl&#39; => true,//是否保留当前URL,默认为true
   &#39;url_append&#39; => &#39;&#39;,
   &#39;keyParam&#39; => &#39;id&#39;, //是否传id,不传设置null
   ],
 ],

],
登录后复制

3、新增的话,这么引用:

<?= Html::a(Yii::t(&#39;yii&#39;, &#39;Create&#39;), [&#39;create&#39;,&#39;returnUrl&#39; => Helper::getReturnUrl()], [&#39;class&#39; => &#39;btn btn-success&#39;]) ?> 。
登录后复制

控制器逻辑

1、用get获取returnUrl,代码:$returnUrl = Yii::$app->request->get('returnUrl'); 。

2、跳转到的URL:return $this->redirect($returnUrl); 。

分析总结

1、此方法的好处是不动核心代码,调用方式保留了Yii2自带的方式。

2、不足之处是在自定义链接的时候需要把每一个操作update、view、delete都写出来,不能用这种'template' => '{view}{update}{delete}'简单且看着舒服能根据需求去写了。

好了,以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作能带来一定的帮助,如果有疑问大家可以留言交流。

以上就是 Yii2实现增删改查后留在当前页的方法详解所有的内容,欢迎在PHP中文网社区讨论和交流问题!

相关推荐:

Yii2中关于组件的注册以及创建的方法详解

Yii2如何使用驼峰命名的形式来访问控制器的实例

Yii2.0框架生成二维码功能的实现代码

Yii2框架自动登录以及登录退出功能的实现方法

以上是Yii2实现增删改查后留在当前页的方法详解的详细内容。更多信息请关注PHP中文网其他相关文章!

相关标签:
来源:php.cn
本站声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板