Hi @azeem202 Do you have two PHP apps one as a frontend and another as a backend? That will be really hard to maintain. Its better to just connect a vuejs application to your main backend app.
Apr 14, 2020
6
Level 1
How to handle API response in controller
Hello, i'm working on eCommerce website 1- project backend and APIs 2- Client side [Frontend] project
1-this is the function in backend project ..
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- and from client side this the function.
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
}
}
please i need help to access error or success message to redirect user correctly.
Please or to participate in this conversation.