使用yii2的基础版本进行开发。
1. 部署
runtimePath 如果有修改 需要 在config/web.php 中置顶新路径 并且确保此路径具有读写权限
应用的静态文件 AppAsset 会在模板布局中使用 也需要保证 web/assets 具有读写权限。
vendorPath 如果依赖的yii2框架文件目录发生变化 需要在 config/web.php 中指定新的 vendorPath 路径
index.php run();
$config = [ 'params' => require(__DIR__ . '/params.php'), //运行环境路径指到公共目录,否则在@app/runtime 并且有写入权限 'runtimePath'=>'app\runtime', 'vendorPath'=>YII_VENDOR_PATH, //如果此路径有变更,必须要在这里配置,否则会出错误];
2. 代码
编写统一的基础展示对象,提供带参数的构造函数和parse方法 填充对象数据。
namespace app\vo;use yii;class BaseVo extends yii\base\Object{ function __construct(array $data=NULL) { if(!empty($data) && is_array($data)){ foreach ($data as $name => $value) { if ($this::hasProperty($name)) { $this->$name = $value; } } } } public function parse(array $data){ if(!empty($data) && is_array($data)){ foreach ($data as $name => $value) { if ($this::hasProperty($name)) { $this->$name = $value; } } } }}
统计的API接口输出类 共有属性可以在json格式化时输出,私有属性不会输出。
code; } /** * @param mixed $code */ public function setCode($code) { $this->code = $code; } /** * @return mixed */ public function getMsg() { return $this->msg; } /** * @param mixed $msg */ public function setMsg($msg) { $this->msg = $msg; } /** * @return mixed */ public function getStartTime() { return $this->start_time; } /** * @param mixed $start_time */ public function setStartTime($start_time) { $this->start_time = $start_time; } /** * @return mixed */ public function getEndTime() { return $this->end_time; } /** * @param mixed $end_time */ public function setEndTime($end_time) { $this->end_time = $end_time; } /** * @return mixed */ public function getData() { return $this->data; } /** * @param mixed $data */ public function setData($data) { $this->data = $data; } /** * 初始化,根据参数返回输出类型 */ public function __construct() { //检查设置 $callback = Yii::$app->request->get('callback'); $varName = Yii::$app->request->get('varname'); switch (true) { case !is_null($callback): if (preg_match("/^[a-zA-Z][a-zA-Z0-9_\.]+$/", $callback)) { $this->setReturnTypeJsonp($callback); break; }else{ //输出错误 } case !is_null($varName): if (preg_match("/^[a-zA-Z][a-zA-Z0-9_\.]+$/", $callback)) { $this->setReturnTypeJsVar($varName); break; }else{ //输出错误 } default: $this->setReturnTypeJson(); break; } $this->start_time=time(); } /** * */ public function setReturnTypeJsVar($varName){ $this->resultFormat = 'jsvar'; $this->reaultVarName = $varName; } /** * */ public function setReturnTypeJson(){ $this->resultFormat = 'json'; } /** * */ public function setReturnTypeJsonp($callback){ $this->resultFormat = 'jsonp'; $this->resultCallBack = $callback; } /** * */ private function toJson (){ return Json::encode($this,JSON_UNESCAPED_UNICODE); } /** * 返回jsonp形式 * var a = {}; */ private function toJsonp (){ return htmlentities($this->resultCallBack).'('.Json::encode($this,JSON_UNESCAPED_UNICODE).');'; } /** * 返回js变量形式 * var a = {}; */ private function toJsVar (){ return 'var '.htmlentities($this->resultCallBack).'=' . Json::encode($this,JSON_UNESCAPED_UNICODE).';'; } /** * 默认 错误 * @param $code * @param string $msg * @return $this */ public function setError($code=RespCode::DEF_ERROR,$msg='失败'){ $this->setCode($code); $this->setMsg($msg); $this->setEndTime(time()); return $this; } /** * 默认成功 * @param string $data * @param number $code * @return $this */ public function setSuccess($data=null, $code=RespCode::SUCCESS){ $this->setCode($code); $this->setData($data); $this->setEndTime(time()); return $this; } /** * */ public function __toString(){ $result = ''; try{ $this->outputHeader(); switch ($this->resultFormat) { case 'json': $result = $this->toJson(); break; case 'jsonp': $result = $this->toJsonp(); break; case 'jsvar': $result = $this->toJsVar(); break; default: break; } }catch (yii\base\Exception $e){ var_dump($e); } return $result; } /** * 根据类型设置编码 */ public function outputHeader(){ switch ($this->resultFormat) { case 'json': header('Content-Type: application/json; charset=utf-8'); break; case 'jsonp': header('Content-Type: application/javascript; charset=utf-8'); break; case 'jsvar': header('Content-Type: application/javascript; charset=utf-8'); break; default: header('Content-Type: text/html; charset=utf-8'); break; } } }