-1

我在电子商务网站上工作

  • 1- 项目后端和 API。2-客户端[前端]项目。

1-这是后端项目中的功能..

public function register(Request $request)
{
    $validator = Validator::make($request->all(), [
        'name' => 'required',
        'email' => 'required|email|unique:users',
        'password' => 'required',
        'mobile' => 'required|unique:users',
        'country_id' => 'required'
    ]);
    if ($validator->fails()) {
        return response()->json(['error'=>$validator->errors()], 401);
    }
    $input = $request->all();
    $input['password'] = bcrypt($input['password']);
    $user = User::create($input);
    $success['token'] =  $user->createToken('MyApp')-> accessToken;
    $success['name'] =  $user->name;
    return response()->json([
        'status' => 'success',
        'data' => $success,
    ], 200);    

}

2-来自客户端的此功能。

protected function register(Request $request)
{
    $response = \Curl::to('http://localhost/backend/public/api/v1/client-register')->withData(
        [
            'name'=> $request->name,
            'email'=> $request->email,
            'password'=> $request->password,
            'mobile'=>$request->mobile,
            'role_id'=>5,
            'country_id'=> $request->country_id
        ])->post();

        $data = json_decode($response, true);

        // what i need here 

        /*check if(response success){

            / go to route('bla bla');

        }else{

            return back with msg 
            please help 
        }**/

}

**请帮助:如果成功,我需要处理响应,如果失败,则返回路由 A,返回消息!

4

1 回答 1

0

你的代码应该是这样的。

protected function register(Request $request)
{
    $response = \Curl::to('http://localhost/backend/public/api/v1/client-register')->withData(
        [
            'name'=> $request->name,
            'email'=> $request->email,
            'password'=> $request->password,
            'mobile'=>$request->mobile,
            'role_id'=>5,
            'country_id'=> $request->country_id
        ])->post();

        $data = json_decode($response, true);

        if(isset($data['status'])) {
            return redirect()->route('bla bla');
        } 

        return redirect::back()->withErrors($data['error']);

}
于 2020-04-15T02:02:18.573 回答