Laravel註冊登錄認證API

註冊接口php

路由api.php:Route::post('register', 'Auth\RegisterController@register');

控制器 http//controllers/auth/registerController

    public function register(\Illuminate\Http\Request $request){

        $validate = $this->validator($request->all());

        if(count($validate->errors()) >0){

            return response()->json(['data'=>$validate->errors()]);
        }

        event(new Registered($user = $this->create($request->all())));

         $this->guard()->login($user);

        return $this->registered($request,$user);
    }

    protected function registered(\Illuminate\Http\Request $request, User $user)
    {
         $user->generateToken();
        return response()->json(['data'=>$user->toArray()],201);
    } 

修改用戶模型裏的參數:app/models/users.php
    public function generateToken(){

        $this->api_token = str_random(60);
        $this->save();
        return $this->api_token;
    }
同時在fillable裏打開這個api_token字段,且在數據庫增長這個字段,vachar 60而後直接訪問 xx.com/api/register 須要POST如下參數name,email,password,password_confirmation
相關文章
相關標籤/搜索