按照官方的写法,我怎么都是提示令牌数据无效
令牌数据无效:
最后各种资料查,最终解决办法如下:
//html//controlleruse think\Controller;class Token extends Controller{ public function index() { if( $this->request->isPost() ) { $data = [ 'account' => $this->request->param('account' , null) , 'password' => $this->request->param('password' , null) , '__token__'=>$this->request->param('__token__',null) ];// dump($data);die; $validate = new \app\test\validate\Token; if( !$validate->check($data) ) { echo $validate->getError(); } else { echo "ok"; } } return $this->fetch(); }}//validatenamespace app\test\validate;use think\Validate;class Token extends Validate{ protected $rule = [ 'account' => 'require|length:6:7|token' , 'password' => 'require|max:6' , ];}
成功:
另一个控制器验证例子 tp5.0.10
public function save(Request $request) {// halt($request->param()); $result = $this->validate( [ 'username' => $request->param('username') , 'password' => $request->param('password') , 'repassword' => $request->param('repassword') , '__token__'=>$request->param('__token__') //必须 ] , [ 'username' => 'require|min:3|unique:user|token' ,//验证user模型唯一 //必须 'password' => 'require|min:3' , 'repassword' => 'require|min:3|confirm:password' ], [ 'username.token'=>'请勿重复提交数据' //令牌不正确的返回信息,默认是“令牌数据无效” ] ); if( true !== $result ) { halt($result); } $user = \app\common\model\User::create([ 'username' => $request->param('username') , 'password' => MD5($request->param('password')) ]); if( !$user ) { halt($user); } else { /*session('uid',$user->id); dump('自动登录'); return redirect('index/user/index');*/ } }
更新数据使用令牌
public function update(Request $request , $id) { //单独更新 $info = \app\common\model\WashCardCate::get(['id' => $id]); $data = input('post.'); $info->title = $data['title']; $info->note = $data['note']; $info->start_time = strtotime($data['start_time']); $info->end_time = strtotime($data['end_time']); $info->count = $data['count']; $info->car_id = implode(',' , $data['car_id']); $info->wash_id = implode(',' , $data['wash_id']); $info->__token__ = input('__token__');//这里 // dump($info);die; $validate = new \app\common\validate\WashCardCate(); $result = $validate->check($info); if( false === $result ) { return $this->error($validate->getError()); } $info->save(); return $this->success('ok' , url('index')); }