azeem202's avatar

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.

0 likes
6 replies
Punksolid's avatar

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.

azeem202's avatar

Hi @Punksolid yes correct, i have two php Apps for the same project, 1 frontend and the other is for backend. the client asked to do this and he not listen to anyone else :(

extjac's avatar
if( $data['status'] == 'success' ) {
//bla bla
}

would not work?

1 like
azeem202's avatar

Fixed

i changed the response message in case fails to.

    if ($validator->fails()) {
        return response()->json(['status'=>false,'message'=>$validator->errors()], 401);
    }

and in case success to. return response()->json([ 'status' => true, 'data' => $success, ], 200);

and in controller i needed to cast Boolean value to string.

          $data = json_decode($response, true);

          if($data ['status'] =="true"){
            dd(true);
          }else{
            dd(false);

          }

so everything working well now. Thank you. have a good day

azeem202's avatar

i needed to make small change for both response if success and fails and cast boolean to string see comment below.

by the way you just gave me the hint to fix it, so thank you so much.

Punksolid's avatar

Well, if the client pays there is no too much choice, but it will be expensive, slow performant and even slower development.

In that case I suggest to do a mirror in frontend and use Repository Classes that will do the fetch, and the controller in client call the methods in the repository class. In that way it will be easier to have everything in order.

1 like

Please or to participate in this conversation.