I am reading through a book called 'Build APIs you wont hate' by Phil Sturgeon. I am trying to following it quite close and i have come up against a little issue.
The methods that i can see in the book, are as follows, these are stored in the basecontroller;
protected function respondWithItem($item, $callback)
{
$resource = new Item($item, $callback);
$rootScope = $this->fractal->createData($resource);
return $this->respondWithArray($rootScope->toArray());
}
protected function respondWithCollection($collection, $callback)
{
$resource = new Collection($collection, $callback);
$rootScope = $this->fractal->createData($resource);
return $this->respondWithArray($rootScope->toArray());
}
protected function respondWithArray(array $array, array $headers = [])
{
return response()->json($array, $this->statusCode, $headers);
}
This works fine if im actually returning an item or a collection, but what about if I want to return one or 2 strings, say a token or a token and message?
In my authentication controller, one of my methods called login, just returns a token and message
$data = ['token' => $token, 'message' => 'User authenticated successfully'];
I know I can simply build the correct JSON response in the authentication controller, but I would like to build all my response methods in one place to make it easier.
via Chebli Mohamed
Aucun commentaire:
Enregistrer un commentaire