AOP编程思想--面向切面编程
(设置了路由后pathinfo失效)
Route::rule('路由表达式','路由地址','请求类型','路由参数(数组)','变量规则(数组)');
请求类型:get post delete put any(默认所有)
use think\Route; Route::rule('test','api/Test/index','GET'); Route::rule('test','api/Test/index','GET | POST');
路由中传递参数
get方式
Route::rule('test/:id','api/Test/index','GET');
public function index($id,$name) { echo "id=".$id."name=".$name; }
访问方式:http://localhost/xcx_shop/public/index.php/test/11?name=xx
post方式
Route::post('test/:id','index/test/index');
use think\Request; param不区分参数来源 获取单个传过来的参数 $id = Request::instance()->param('id'); 获取所有传过来的参数 $all = Request::instance()->param(); 获取get方式传过来的参数 $all = Request::instance()->get(); 获取路径中传过来的参数 $all = Request::instance()->route(); 获取post方式传过来的参数 $all = Request::instance()->post(); ....
input('param.');
use think\Request; public function test(Request $request) { $all = $request->param(); } //$request 不需要关注从哪里来的
1.独立验证
$validate = new Validate([ 'pwd' => 'require', 'new_pwd' => 'require', 'confirm_pwd' => 'require|confirm:new_pwd' ],[ 'pwd.require' => '请输入原始密码', 'new_pwd.require' => '请输入新密码', 'confirm_pwd.require' => '请重复新密码', 'confirm_pwd.confirm' => '确认密码与新密码不一致' ] ); if (!$validate->check($data)) { return ['valid'=> 0,'msg'=>$validate->getError()]; } //原始密码是否正确 $userInfo = $this->where('pwd',$data['pwd'])->where('id',session('admin_id'))->find(); if(!$userInfo){ return ['valid'=>0, 'msg'=>'原始密码不正确']; } //修改密码 // save方法第二个参数为更新条件 $res = $this->save([ 'pwd' =>$data['new_pwd'], ],[$this->pk => session('admin_id')]); if($res){ return ['valid'=>1, 'msg'=>'密码修改成功']; }else{ return ['valid'=>0, 'msg'=>'密码修改失败']; }
2.验证器
新建TestValideta.php
'require', 'new_pwd' => 'require', 'confirm_pwd' => 'require|confirm:new_pwd' ]; protected function isInt($value,$rule='',$data,$field='') { if(is_number($value) && is_int($value +0) && ($value +0)>0){ return true; }else{ return $field."必须是正整数"; } } } 使用: $validate = new TestValidate(); $result = $validate ->batch()->check($data);
3.自定义验证规则(如上)
protected function isInt($value,$rule='',$data,$field='') { if(is_number($value) && is_int($value +0) && ($value +0)>0){ return true; }else{ return $field."必须是正整数"; } }
查看更多文章请访问:https://www.wangmingchang.com/