/** * Bootstrap the application services. * * @return void */ public function boot() { Response::macro('success', function ($data) { return Response::json(['errors' => false, 'data' => $data]); }); Response::macro('error', function ($message, $status = 400) { return Response::json(['message' => $status . ' error', 'errors' => ['message' => [$message]], 'status_code' => $status], $status); }); }
/** * Build the response macro. * * @param string $name * @param string|null $method * @return void */ protected function buildResponseMacro($name, $method = null) { $method = $method ?: $name; $app = $this->app; Response::macro($name, function ($value, TransformerAbstract $transformer, $keys = [], SerializerAbstract $serializer = null) use($app, $method) { $response = $app[ResponseFactory::class]; $method = $value instanceof LengthAwarePaginator ? 'paginator' : $method; $serializer = $serializer ?: new JsonNormalizeSerializer(); return $response->{$method}($value, $transformer, $keys, function ($resource, $fractal) use($app, $method, $serializer) { if ($method != 'paginator') { $fractal->setSerializer($serializer); } $with = $app['config']->get('apihelper.prefix', ''); $with .= 'with'; if ($includes = $app['request']->input($with)) { $fractal->parseIncludes($includes); } }); }); }
/** * Define Larave Response Macros * * @return void */ private function defineMacros() { # Resources Response::macro('collection', function ($collection, $callback) { return Facades\Restful::collection($collection, $callback); }); Response::macro('single', function ($model, $callback) { return Facades\Restful::single($model, $callback); }); Response::macro('created', function ($model, $callback) { return Facades\Restful::created($model, $callback); }); Response::macro('updated', function ($model, $callback) { return Facades\Restful::updated($model, $callback); }); Response::macro('deleted', function ($message) { return Facades\Restful::deleted($message); }); Response::macro('success', function ($message) { return Facades\Restful::success($message); }); # Errors Response::macro('validationFailed', function (array $errors = [], $message = 'Validation Failed') { return Facades\Restful::validationFailed($errors, $message); }); Response::macro('unprocessable', function ($message) { return Facades\Restful::unprocessable($message); }); Response::macro('forbidden', function ($message) { return Facades\Restful::forbidden($message); }); Response::macro('unauthorized', function ($message) { return Facades\Restful::unauthorized($message); }); Response::macro('notFound', function ($message = 'Resource Not Found') { return Facades\Restful::notFound($message); }); }
/** * Boot the response facade macro. * * @return void */ protected function bootResponseMacro() { Response::macro('api', function () { return $this->app['dingo.api.response']; }); }
/** * Bootstrap the application events. * * @return void */ public function boot() { $this->package('vena/jsend4laravel', 'jsend4laravel'); /** * Wrapper for sending a JSON response with automatic JSONP support * @param string $message (required) Message describing the error. * @param string|array $data (optional) Data to include in the response. * @param int $status (optional) HTTP status code of the return response. * @param array $headers (optional) Request headers to include in the response. * @return Response */ Response::macro('jsend', function ($data = NULL, $httpStatus = 200, array $headers = array()) { $response = Response::json($data, $httpStatus, $headers); if (Input::has('callback')) { $response->setCallback(Input::get('callback')); } return $response; }); /** * Return a JSend success response * * @param string $message (required) Message describing the error. * @param string|array $data (optional) Data to include in the response. * @param int $status (optional) HTTP status code of the return response. * @param array $headers (optional) Request headers to include in the response. * @return Response */ Response::macro('jsendSuccess', function ($data = NULL, $httpStatus = 200, array $headers = array()) { return Response::jsend((object) array('status' => 'success', 'data' => $data), $httpStatus, $headers); }); /** * Return a JSend fail response * * @param string $message (required) Message describing the error. * @param string|array $data (optional) Data to include in the response. * @param int $status (optional) HTTP status code of the return response. * @param array $headers (optional) Request headers to include in the response. * @return Response */ Response::macro('jsendFail', function ($data, $httpStatus = 400, array $headers = array()) { $responseData = (object) array('status' => 'fail', 'data' => $data); return Response::jsend($responseData, $httpStatus, $headers); }); /** * Return a JSend error response * * @param string $message (required) Message describing the error. * @param int $code (optional) An internal error code, if applicable * @param string|array $data (optional) Data to include in the response. * @param int $status (optional) HTTP status code of the return response. * @param array $headers (optional) Request headers to include in the response. * @return Response */ Response::macro('jsendError', function ($message = NULL, $code = NULL, $data = NULL, $httpStatus = 400, array $headers = array()) { if (is_null($message)) { throw new \BadMethodCallException($this->app['translator']->get('jsend4laravel::messages.jsend_errors_must_have_messages')); } $responseData = (object) array('status' => 'error', 'message' => $message); if (!is_null($code)) { $responseData->code = $code; } if (!is_null($data)) { $responseData->data = $data; } return Response::jsend($responseData, $httpStatus, $headers); }); }