A
A
Artem00712018-01-31 12:33:39
Laravel
Artem0071, 2018-01-31 12:33:39

How to make a response template?

I'm learning Laravel.
I'm making API for my web project on it.
Previously, everything was on a self-written bicycle with the following structure of the answer:

{success: true,
globe_error: 'OK',
errors: [],
data: []}

How to make the same response template in Laravel? Or is it even possible?
Previously, I just had an Output class with static methods '::addError', '::setData'
Then it was all run through json_encode and I was enjoying life
. And here I already googled everything, but did not find anything similar

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Andreo, 2018-01-31
@Artem0071

Try to create a trait with your methods that form the desired response format, and inject this trait into the base controller. After that, you can successfully return the required response in all controllers like this:
return $this->success($message);

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question