# 控制器
控制器不强制继承任何类,但只有继承于start\Controller才能使用框架拓展的属性和方法
# 控制器定义
控制器文件通常放在controller下面,采用首字母大写的驼峰命名法命名
根据分层调用规范,控制器仅能调用服务层,故与模型名保持一致即可,无须添加后缀
- 命令行创建
php start make:controller demo@Article
<?php
namespace app\demo\controller;
use start\Controller;
/**
* 文章管理
* @auth
*/
class Article extends Controller
{
/**
* 查看记录
* @auth
*/
public function page()
{
// 失败返回
$this->error();
// 成功返回
$this->success();
}
}
# 注解权限菜单
只需在对应接口上添加权限标签即可完成权限控制,如上控制器所示
注解格式: @[标签] [参数]
构建命令: php start auth [应用名]
框架所支持的注解标签如下:
- @auth 注解后需要授权角色权限才可访问
- 可选参数:json格式数据,如{"icon": "list", "is_menu": 0}
- @super 注解后仅限超管员身份访问
- 可选参数:无
- @admin 注解后管理员身份即可访问
- 可选参数:无
- @login 注解后登录即可访问
- 可选参数:无
- @menu 注解后在菜单中显示
- 可选参数:无
- @view 注解视图模板路径
- 可选参数:模板文件路径,如;/article/page
- 默认路径:应用名/控制器名/方法名
- @open 注解后作为开放路由返回
- 可选参数:无
- 注:完全不添加注解标签时接口是开放访问的,但权限节点不会返回给前端,添加open标签后会返回
/**
* 文章管理
* @auth {"icon": "list"}
* @menu
* @view article/page
*/
class Article extends Controller
{
/**
* 查看记录
* @auth
*/
public function page()
{
// 失败返回
$this->error();
// 成功返回
$this->success();
}
}
# 注解接口文档
StartCMS完全支持apidoc接口文档注释,具体注释方法请看ApiDoc (opens new window)
文档构建命令:php start apidoc [应用名]
/**
* 文章管理
* @auth {"icon": "list"}
* @menu
* @view article/page
*/
class Article extends Controller
{
/**
* 查看记录
* @auth
*
* @api {get} article/page 查看记录
* @apiName page
* @apiGroup Article
* @apiVersion v1.0.0
* @apiDescription 获取分页数据
* @apiUse CommonHeader
* @apiUse CommonParam
* @apiUse PagingParam
* @apiParam {string} [keyword] 关键词
* @apiUse CommonSuccess
* @apiUse CommonError
*/
public function page()
{
// 失败返回
$this->error();
// 成功返回
$this->success();
}
}
# 接口验证方法
继承于start\Controlelr的控制器均支持formValidate参数验证方法
<?php
namespace app\demo\controller;
use start\Controller;
/**
* 文章管理
* @auth
*/
class Article extends Controller
{
/**
* 查看记录
* @auth
*/
public function page()
{
/**
* formvValidate参数
* $rules: 验证规则
* $strict: 严格模式,开启后仅返回经过规则验证的参数
* $type: 请求方法,用于指定post或get方法,一般获取信息接口用get,更新信息接口为post
*/
$input = $this->formValidate([
'title.require' => '不能为空', // require表示必填
'keyword.default' => '', // default表示默认值
'is_deleted.value' => 0, // value表示固定值不能修改
'status.integer|ifexist' => 'status仅限数字类型' // integer|ifexist表示存在则验证
],true,post);
// 失败返回
$this->error();
// 成功返回
$this->success();
}
}
内置规则
- 'require' => ':attribute require',
- 'must' => ':attribute must',
- 'number' => ':attribute must be numeric',
- 'integer' => ':attribute must be integer',
- 'float' => ':attribute must be float',
- 'boolean' => ':attribute must be bool',
- 'email' => ':attribute not a valid email address',
- 'mobile' => ':attribute not a valid mobile',
- 'array' => ':attribute must be a array',
- 'accepted' => ':attribute must be yes,on or 1',
- 'date' => ':attribute not a valid datetime',
- 'file' => ':attribute not a valid file',
- 'image' => ':attribute not a valid image',
- 'alpha' => ':attribute must be alpha',
- 'alphaNum' => ':attribute must be alpha-numeric',
- 'alphaDash' => ':attribute must be alpha-numeric, dash, underscore',
- 'activeUrl' => ':attribute not a valid domain or ip',
- 'chs' => ':attribute must be chinese',
- 'chsAlpha' => ':attribute must be chinese or alpha',
- 'chsAlphaNum' => ':attribute must be chinese,alpha-numeric',
- 'chsDash' => ':attribute must be chinese,alpha-numeric,underscore, dash',
- 'url' => ':attribute not a valid url',
- 'ip' => ':attribute not a valid ip',
- 'dateFormat' => ':attribute must be dateFormat of :rule',
- 'in' => ':attribute must be in :rule',
- 'notIn' => ':attribute be notin :rule',
- 'between' => ':attribute must between :1 - :2',
- 'notBetween' => ':attribute not between :1 - :2',
- 'length' => 'size of :attribute must be :rule',
- 'max' => 'max size of :attribute must be :rule',
- 'min' => 'min size of :attribute must be :rule',
- 'after' => ':attribute cannot be less than :rule',
- 'before' => ':attribute cannot exceed :rule',
- 'expire' => ':attribute not within :rule',
- 'allowIp' => 'access IP is not allowed',
- 'denyIp' => 'access IP denied',
- 'confirm' => ':attribute out of accord with :2',
- 'different' => ':attribute cannot be same with :2',
- 'egt' => ':attribute must greater than or equal :rule',
- 'gt' => ':attribute must greater than :rule',
- 'elt' => ':attribute must less than or equal :rule',
- 'lt' => ':attribute must less than :rule',
- 'eq' => ':attribute must equal :rule',
- 'unique' => ':attribute has exists',
- 'regex' => ':attribute not conform to the rules',
- 'method' => 'invalid Request method',
- 'token' => 'invalid token',
- 'fileSize' => 'filesize not match',
- 'fileExt' => 'extensions to upload is not allowed',
- 'fileMime' => 'mimetype to upload is not allowed',