Laravel Package For Standard API Responses

preview_player
Показать описание
Instead of calling response() - json() with different unpredictable structures and status codes, you may use more readable syntax, with this package.

- - - - -
Support the channel by checking out my products:

- - - - -
Other places to follow:
Рекомендации по теме
Комментарии
Автор

I personally think you shouldn't use packages for things these small, at least not if you're working on medium to large scaled projects. This just causes too much overhead later on when updating your framework version.

epicgameryt
Автор

I like responses with macros. It allow to overwrite exception and middleware responses too

guillermofelipetti
Автор

It’s better using response macros. Then:
response()->success($data)
response()->error(…)

ivanmorozov
Автор

I prefer using response macros and have things like response()->created(), response()->success() etc.Learnt that from you as well Mr Povilas.

olusolaojewunmi
Автор

I have several microservices built that are consuming API and they all have to have the same standardized way of getting/providing a response. A package like this would have been a life saver.

roberttudor_
Автор

I also make this kind of shortcut in all of my API laravel projects using traits,

for me, it's best to make it manually rather than installing the package because in my case I have to throw different json structures for each project, also I don't particularly appreciate editing the package's code

justsomecuriosperson
Автор

I think its better to add it directly in Http/Controllers/Controller class

jopaymaymay
Автор

I would prefer to create a method in the base Controller class. Since all the controllers are extended from it the response method can be used in all the controllers.

adarshchacko
Автор

Great recommendation, thanks to get us this information ❤

angelp
Автор

Useful, however, creating custom response classes via Laravel Responsable looks more flexible as for me. Thus we can avoid an obsolete package dependency and customise response structure per each project.

ruslanvoroshchukowlookitlt
Автор

thanks man I just looking for something like this

miladganjali
Автор

I'm just using Traits
make a function like returnErrorResponse($message, $status_code) if there is an error in try catch or whatever and just return it in the controller

zwkjxdz
Автор

Mostly 200 and 201 responses gets to my controller
Others are handled by the exception handler, all responses are formatted using a similar trait pattern

Bbtechjourney
Автор

We can implement by creating a custom like BaseApiController and implements same like methods over there.. 😉

prameshdhakal
Автор

Good information, I was wondering how to implement OAuth 2.0 authentication with laravel/passport.

mddwzhf
Автор

My xampp is running well and i am not getting any error but why does the "PHP artisan migrate" command not create new tables in my database, except by default tables users, migrations and password resets? If t have solution, feel free to help me.

badalsinghania
Автор

personally i create macro over response to achieve the same result

giacomogaravaglia
Автор

I prefer to use Macro to return API response success, fail, .. etc.

omarmuhtaseb
Автор

I always add this inside the main Controller.php:

public function sendResponse(mixed $result = '', string $message = '', int $code = 200): JsonResponse
{
$response = [
'success' => true,
'result' => $result,
'message' => $message,
];

return response()->json($response, $code);
}

public function sendErrorResponse(string $message, array $errors = [], int $code = 404): JsonResponse
{
$response = [
'success' => false,
'message' => $message,
'errors' => $errors,
];

return response()->json($response, $code);
}

}

Then if I need another kind of response, like retrieving images, I create something like this:

public function sendPNGResponse(string $png, string $message = '', int $code = 200): Response
{
return response($png, $code, [
'Content-Type' => 'image/png',
]);
}

mauriciomueller
Автор

I just have a trait that does this. Never needed a package.

l.b