Laravel Return Types in API Controller: Five Options?

preview_player
Показать описание
I was wondering what should API Controller methods return. Let me show you what I found in this experiment.

Links mentioned in the video:

Support the channel by checking out my products:

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

I consistently return either a JsonResponse or a Response (for a 204 status) as the function’s return type. It’s always a good practice to enforce typing everywhere, including on function arguments.

gtsmeg
Автор

good to see you back uncle Povs, happy new year ✨

ricko
Автор

I actually like the JsonResource type in this case. Thank you for sharing. I learn something new every day from your post.

nisone-k
Автор

Welcome back, I hope you and your family had a good holiday and new year. I'm definitely one for return types in controllers. I tend to collapse methods in my editor when not actively working on or reviewing them in depth, so the return type also adds some visual information and just feels "right". But I can also understand why they could be seen as redundant, as it's pretty standard for the `index()` or `show()` method to always return a view, `store()` to always return a redirect, etc. But as you say, it's a personal preference.

JJASMR
Автор

We wrap resource (or collection) into response()->json() and always return JsonResponse from controller.

Илья-щфи
Автор

From my point of view there are a few mistakes with the final takeaway.

First of all "mixed" is not useless, but indicates "Hey we cannot specify this right now, but we have thought about it"
as opposed to having forgotten a return type completely.

Furthermore, if anything the Framework should define the return type of all controllers like "{ [key :string] : (...args: any[]) => Responsable }" (which i do not think is possible right now in php), but like that it is easier for me as a developer to know what i am allowed to return with laravel being able to handle it correctly.
Therefor i would have preferred the stubs to use Responsable, i think.

And finally, i think this is where Laravel breaks down or rather its approach to do so much "Magic" and "under the hood conversion".
It makes it harder to really define what is expected as a result. In most cases i dislike magic methods, besides the "fancy" factor.
If you want to have good type hints they become obsolete as instead of writing the function, you have to annotate the property/method instead.

My suggestion would be to have more strict expectations in return types.
But have some methods that allow various many to one conversions like for example "makeResponsable", which takes anything a controller can return currently and returns a strict type, ready to be send back.

object_name
Автор

It's always good to have return types from methods and that is the best practice from strong typed languages that PHP (Laravel) should follow in my humble opinion. It's helpful for many reasons.

donmikele
Автор

Thanks for sharing, I was searching for this.

Tyche.Crypto
Автор

I usually do
```
return response()->json([

]);
```
it does feel repetetive . what do you think?

AbderrahmanFodili
Автор

I do return a resource or resource collection but like so
return response()->json(['data' => ModelResource::make($model), ...]);
So i get the JsonResponse return type

mortezaDev
Автор

I use static analysis tools and they require return types on everything, so my controllers look like this:

public function index(Request $request): JsonResponse|View
{
if ($request->expectsJson()) {
$data = $this->service->paginate(request()->integer('perPage', 10));

return response()->json($data);
}

return view('index');
}

BsiennKhan
Автор

Always returning JsonResponse or JsonResource

trewlove
visit shbcf.ru