当前位置: 首页 > news >正文

十大免费ae模板网站wordpress 远程设置

十大免费ae模板网站,wordpress 远程设置,东庄水利建设公司网站,简约 网站模板如题#xff0c;在实战项目中我们我们经常会要使用相同的逻辑#xff0c;比如post/get获取参数#xff0c;接口返回、参数校验等#xff0c;因此可以将其封装起来调用。 这里分为根目录下的控制器controllers和处理数据库的models两个文件#xff0c;其他业务逻辑放在modu…如题在实战项目中我们我们经常会要使用相同的逻辑比如post/get获取参数接口返回、参数校验等因此可以将其封装起来调用。 这里分为根目录下的控制器controllers和处理数据库的models两个文件其他业务逻辑放在modules模块中 一、根目录下controllers // BaseController.php ?php namespace app\controllers; use yii\web\{Controlle, IdentityInterface, Response}; use Yii;// 继承 yii\web\controller, 其他controller则继承BaseController class BaseController extends Controller {/*** 错误码*/protected $code ;/*** 错误消息*/protected $msg success;/*** 响应报文*/protected $rspData [];/*** 模型*/protected $model null;/*** 路由*/protected $route;public function __construct($id, $module, $config []){$this-route Yii::$app-requestedRoute;Yii::info(json_encode($this-request(), JSON_UNESCAPED_SLASHES), [ERQUEST BODY] $this-route);parent::__construct($id, $module, $config);}/*** 设置拦截器*/public function behaviors(): array{return [];}public function init(){parent::init();}/*** 获取get参数*/protected function get($key ){$get $key ? Yii::$app-request-get($key) : Yii::$app-request-get();return $get;}/*** 获取 post 参数*/protected function post($key ){$post $key ? Yii::$app-request-post($key) : Yii::$app-request-post();return $post;}/*** 获取 get、post 参数*/protected function request($key ){$get $this-get($key);$post $this-post($key);if (is_array($get) is_array($post)) {return array_merge($get, $post);}return $post ? $post : ($get ?: null);}/*** 返回当前用户*/public function getUserIdentity(): ?IdentityInterface{return Yii::$app-getUser()-identity;}/*** 前置操作*/public function beforeAction($action): bool{$params $this-post();/*** 分页、排序、参数处理*/if ($this-model !empty($params[page])) {$page intval($params[page]);if ($page 1) {$page 1;}$this-model-page $page;}if ($this-model !empty($params[pageSize])) {$pageSize Intval($params[pageSize]);$this-model-pageSize $pageSize;}if ($this-model !empty($params[_sorting])) {$sorting [];if (!is_array($params[_sorting])) {$params[_sorting] json_decode($params[_sorting], true);}foreach ($params[_sorting] as $item) {$sorting[] $item[field] . . $item[type];}$this-model-$sorting implode(,, $sorting);}return parent::beforeAction($action);}/*** 判断是否是get方法*/protected function isGet(){return Yii::$app-request-isGet;}/*** 判断是否是post方法*/protected function isPost(){return Yii::$app-request-isPost;}/*** 组装JSON格式响应** param array $data* return Response*/public function asJson($data []): Response{return parent::asJson([code $this-code, msg $this-msg, data $data]);}protected function isAjax(){return Yii::$app-request-isAjax;}protected function littleToHump($word): string{$separator _;$word $separator . str_replace($separator, , $word);return ltrim(str_replace( , , ucwords($word)), $separator);}/*** 响应数据*/protected function response($data []): array{Yii::$app-response-format Response::FORMAT_JSON;if ($this-code) {$rsp [code $this-code,msg $this-msg,data $this-rspData ?: $data];} else {$rsp [code 00000,msg $this-msg ?: success,data $this-rspData ?: $data];}return $rsp;}/*** 后置操作*/public function afterAction($action, $result){return parent::afterAction($action, $result);}/*** 验参*/public function paramsValidate(array $params, array $fieldRules, string $translator default, string $tag ): bool{// field_validator 来自web/index.php中全局注册的components/helper/function.php$res field_validator($params, $fieldRules, $translator, $tag);if (!empty($res)) {// 将校验参数的结果赋值给code, msg[$this-code, $this-msg] $res;return false;}return true;} }?二、根目录下models // BaseModel.php // 继承yii\db\ActiveRecord, 增删改查操作数据库 ?php namespace app\models;use yii\db\{ActiveRecord, ActiveQuery, Connection, Exception}; use Yii;abstract class BaseModel extends ActiveRecord {public $page 1; // 查询limit页码public $pageSize 20;protected static $insertId 0; // 插入数据的的ID/*** 调用父依赖构造函数** param array $config*/public function __construct($config []){parent::__construct($config);}/*** 数据库实例* return Connection interface*/public function dbInstance(): Connection{$db static::getDb(); // ActiveRecord方法连接dbif ($db) {return $db;}return Yii::$app-db; // 默认全局db}/*** 查询对象* return ActiveQuery interface*/public function query(): ActiveQuery{return self::find(); // ActiveRecord方法查询}/*** 组装查询对象* * param array $conditions 条件集合数组* param array $fields 查询字段* param array $expression 条件连接符* param array $table 指定数据表* * return ActiveQuery interface*/public function queryDbAll(array $conditions [], string $fields *, string $expression and, string $table ): ActiveQuery{// 兼容一维数组if (empty(array_filter($conditions, function ($v, $k) {return is_array($v) is_numeric($k);}, ARRAY_FILTER_USE_BOTH))) {$conditions [$conditions];}if (empty($table)) {$query self::find()-select($fields);} else {$query self::find()-from($table)-select($fields);}// array_filter默认去除false元素foreach (array_filter($conditions) as $k $v) {if ($k 0) {$query-where($v);continue;}switch ($expression) {case or:$query-orWhere($v);case and:default:$query-andWhere($v);}}// echo $query - createCommand() - getRawSql();return $query;}/*** 获取单条记录*/public function getDbOne(array $conditions [], string $fields *, string $expression and): ?array{// 兼容一维数组if (empty(array_filter($conditions, function ($v, $k) {return is_array($v) is_numeric($k);}, ARRAY_FILTER_USE_BOTH))) {$conditions [$conditions];}$query self::find()-select($fields);foreach (array_filter($conditions) as $k $v) {if ($k 0) {$query-where($v);continue;}switch ($expression) {case or:$query-orWhere($v);break;case and:default:$query-andWhere($v);break;}}if (in_array(id, static::primaryKey())) {$query-orderBy([id SORT_DESC]);}$res $query-asArray()-one();if (!empty($res)) {camel_snake($res);}return $res;}/*** 获取全部记录* param array $conditions 表示查询条件默认无条件查询* param string $fields 表示返回的字段默认全部返回消耗接口流量宽带* param array $order 表示排序默认id降序* param boolean $limit 表示数据条数默认取全部* param array $groupBy 表示列中同值分组默认不分组* param string $expression 表示条件关系默认and*/public function getDbAll(array $conditions [], string $fields *, array $order [id SORT_ASC], bool $limit false, array $groupBy [], string $expression and): array{/*** 兼容一维数组最终转出二维数组* array_filter() ARRAY_FILTER_USE_BOTH表示回调可以接收两个参数 $v, $k* 当前array_filter表示过滤获取二维数组如果得到空数组则表示非二维数组一维数组需要转出二维数组*/if (empty(array_filter($conditions, function ($v, $k) {return is_array($v) is_numeric($k);}, ARRAY_FILTER_USE_BOTH))) {$conditions [$conditions];}$query self::find()-select($fields);foreach (array_filter($conditions) as $k $v) {if ($k 0) {$query-where($v);continue;}switch ($expression) {case or:$query-orWhere($v);break;case and:default:$query-andWhere($v);}}// dd(11, $conditions, $fields, $query);if (!empty($groupBy)) {$query-groupBy($groupBy);}if (!empty($order)) {$query-orderBy($order);}if ($limit) {$query-limit($this-pageSize)-offset(bcmul($this-pageSize, bcsub($this-page, 1)));}$res $query-asArray()-all();array_walk($res, function ($v) {if (!empty($v)) {camel_snake($v);}});return $res;}/*** 获取记录行*/public function getCount(array $conditions [], string $expression and): int{// 兼容一维数组if (empty(array_filter($conditions, function ($v, $k) {return is_array($v) is_numeric($k);}, ARRAY_FILTER_USE_BOTH))) {$conditions [$conditions];}$query self::find();foreach (array_filter($conditions) as $k $v) {if ($k 0) {$query-where($v);continue;}switch ($expression) {case or:$query-orWhere($v);break;case and:default:$query-addWhere($v);}}return $query-count();}/*** 添加记录*/public function create(array $data): int{$rows $this-dbInstance()-createCommand()-insert(static::tableName(), $data)-execute();// 记录自增IDself::$insertId $this-dbInstance()-getLastInsertID();return $rows;}/*** 批量添加*/public function batchCreate(array $fields, array $data): int{if (empty($fields) || empty($data)) {throw new Exception(Missing batch fields or data);}$rows $this-dbInstance()-createCommand()-batchInsert(static::tableName(),$fields,$data)-execute();self::$insertId $this-dbInstance()-getLastInsertID();return $rows;}/*** 获取插入的自增ID*/public function getInsertId(): int{return self::$insertId;}/*** 更改记录* $data 新的数据* $conditions 查询条件的集合如[id id, sex man]*/public function updateByCondition(array $data, array $conditions): int{if (empty($data) || empty($conditions)) {throw new Exception(Missing data or conditions);}return $this-dbInstance()-createCommand()-update(static::tableName(),$data,$conditions)-execute();}/*** 指定主键删除记录*/public function deleteById(int $id): void{if (empty($id)) {throw new Exception(Missing primary key);}self::findOne($id)-delete();}/*** 指定删除记录*/public function deleteData(array $params): void{if (empty($params)) {throw new Exception(Missing primary key);}self::findOne($params)-delete();}/*** 判断数据表是否存在*/public function tableExist(string $tableName): bool{return !empty($this-dbInstance()-getTableSchema($tableName));} }?
http://www.zqtcl.cn/news/778793/

相关文章:

  • 青岛网站的优化云南抖音推广
  • 做中英文版的网站需要注意什么如何偷别人dedecms网站的模板
  • 免费微网站制作最近三天发生的重要新闻
  • 网站优化网络推广seo编程软件python
  • 建设部网站官网合同免费申请网站永久
  • 遵化建设局网站哈尔滨网站制作公司价格
  • 科技因子网站建设方案河南网站推广优化公司
  • 什么网站了解国家建设的行情如何建设自己的php网站
  • 大连市平台网站外包公司和劳务派遣
  • 广州建网站公司排名嵌入式软件开发工程师工作内容
  • 计算机软件网站建设免费asp网站源码
  • 网站建设介绍ppt镇江网站搜索引擎优化
  • 珠海自助建站软件泉州网站开发
  • ios个人开发者账号多少钱拼多多seo怎么优化
  • 五金网站建设信息产业部备案网站
  • 网站被百度惩罚放弃互联网平台宣传推广方案
  • 自己怎么做网站首页自动app优化
  • 图形设计网站泉州网站建设企业
  • 免费建各种网站有没有做网站的团队
  • 做网站做网站的公司电商网站怎么做
  • 福建专业网站建设公司《设计》韩国
  • 怎么区分网站是模板做的Wordpress福利资源模板
  • 文案类的网站最新域名网站
  • 网站seo优化效果智能营销系统开发
  • 国外做储物的网站个人网站建设在哪里
  • 北京高端网站设计外包公司不用代码做网站的工具
  • 网站开发交付资料广告设计公司经营范围
  • 如何建立一个好的网站wordpress 看不到主题
  • 古典网站织梦模板云南app软件开发
  • 网页设计与网站建设期末考试wordpress文章页面图片自动适应