在 API 中,将路由创建为
Route::post('register','Api\UsersController@create');
在用户控制器中创建方法create()
function create(Request $request)
{
/**
* Get a validator for an incoming registration request.
*
* @param array $request
* @return \Illuminate\Contracts\Validation\Validator
*/
$valid = validator($request->only('email', 'name', 'password','mobile'), [
'name' => 'required|string|max:255',
'email' => 'required|string|email|max:255|unique:users',
'password' => 'required|string|min:6',
'mobile' => 'required',
]);
if ($valid->fails()) {
$jsonError=response()->json($valid->errors()->all(), 400);
return \Response::json($jsonError);
}
$data = request()->only('email','name','password','mobile');
$user = User::create([
'name' => $data['name'],
'email' => $data['email'],
'password' => bcrypt($data['password']),
'mobile' => $data['mobile']
]);
// And created user until here.
$client = Client::where('password_client', 1)->first();
// Is this $request the same request? I mean Request $request? Then wouldn't it mess the other $request stuff? Also how did you pass it on the $request in $proxy? Wouldn't Request::create() just create a new thing?
$request->request->add([
'grant_type' => 'password',
'client_id' => $client->id,
'client_secret' => $client->secret,
'username' => $data['email'],
'password' => $data['password'],
'scope' => null,
]);
// Fire off the internal request.
$token = Request::create(
'oauth/token',
'POST'
);
return \Route::dispatch($token);
}
创建新用户后,返回访问令牌。