Reputation: 897
I'm returning some JSON data as response in a controller that I want to add some information to.
In my DriversController
extend's Apicontroller
in DriversController
I'm returning some data on API call, iI want to append the status code information to below response:
if ($request->wantsJson()) {
return Response::json([
'data' => [
'user_details' => $agent_percentage,
'dropdown_data' => [
'employment_types' => $employment_types->all(),
'roles' => $roles->all(),
'vehicle_brands' => $vehicle_brands->all(),
'vehicle_types' => $vehicle_types->all()
]
]
]);
}
//to the above response
return Response::json([
$this->respondSuccess(), // i am append this information
'data' => [
'user_details' => $agent_percentage,
'dropdown_data' => [
'employment_types' => $employment_types->all(),
'roles' => $roles->all(),
'vehicle_brands' => $vehicle_brands->all(),
'vehicle_types' => $vehicle_types->all()
]
]
]);
In ApiControllre
I'm setting all the status code and messages
class ApiController extends Controller
{
protected $statusCode = 200;
//getter status code
public function getStatusCode()
{
return $this->statusCode;
}
//setter status code
public function setStatusCode($statusCode)
{
$this->statusCode = $statusCode;
return $this;
}
//failure messages
public function respondFailure($message='Account is not active contact admin', $status='failure')
{
return $this->setStatusCode(400)->respondWithMessage($message, $status);
}
//success messages
public function respondSuccess($message='Agent is active', $status='success')
{
return $this->setStatusCode(200)->respondWithMessage($message, $status);
}
//a layer of abstraction to avoide repetation
public function respond($data, $headers = [])
{
return Response::json($data, $this->getStatusCode(), $headers);
}
//get ststus code and message parse it for errors
public function respondWithMessage($message, $status)
{
return $this->respond([
'status_code' => $this->getStatusCode(),
'status' => $status,
'message' => $message
]);
}
}
But the response I'm getting is different as expected:
//expected result
{
"status_code": "200",
"status": "success",
"message": "User details with dropdown data",
"data": {
"user_details": {
"id": 2017001,
"name": "User Name",
"email": "[email protected]",
},
"dropdown_data": {
}
}
}
//getting response
{
"0": {
"headers": {},
"original": {
"status_code": 200,
"status": "success",
"message": "Agent is active"
},
"exception": null
},
"data": {
"user_details": {
"id": 2017001,
"name": "User Name",
"email": "[email protected]",
},
"dropdown_data": {
}
}
}
the middleware
<?php
namespace App\Http\Middleware;
use Closure;
use Response;
use App\Http\Controllers\ApiController;
class UserStatus extends ApiController
{
/**
* Handle an incoming request.
*
* @param \Illuminate\Http\Request $request
* @param \Closure $next
* @return mixed
*/
public function handle($request, Closure $next)
{
if($request->user() === null)
{
return $this->respondFailure();
}
if($request->user()->isActive($request->user()))
{
return $next($request);
}
return $this->respondFailure();
}
}
Upvotes: 1
Views: 9788
Reputation: 4964
You are only appending the response from the respondSuccess()
and not merging the response.
$this->setStatusCode(200)->respondWithMessage($message, $status);
on this response:
return Response::json([
$this->respondSuccess(), // i am append this information
'data' => [
'user_details' => $agent_percentage,
'dropdown_data' => [
'employment_types' => $employment_types->all(),
'roles' => $roles->all(),
'vehicle_brands' => $vehicle_brands->all(),
'vehicle_types' => $vehicle_types->all()
]
]
]);
It gives a response other than the response you have expected.
To get the expected response you need to do something like this:
public function respondWithMessage($message, $status)
{
return [
'status_code' => $this->getStatusCode(),
'status' => $status,
'message' => $message
];
}
I have used only array and not $this->respond()
because you only have this message:
"status_code": "200",
"status": "success",
"message": "User details with dropdown data",
For the type of response, you might need to merge the two arrays into one. Look on array_merge() to get more understanding.
$responseMessage= $this->respondSuccess();
$data = ['data' => [
'user_details' => $agent_percentage,
'dropdown_data' => [
'employment_types' => $employment_types->all(),
'roles' => $roles->all(),
'vehicle_brands' => $vehicle_brands->all(),
'vehicle_types' => $vehicle_types->all()
]
]
];
$responseArray = array_merge(responseMessage, data);
return Response::json($responseArray);
I have not yet tested the code but this might give you some understanding of how to get the expected array response you want.
If I am wrong anyone could suggest the edit.
Upvotes: 3