G
G
Glory2021-12-15 15:18:08
Laravel
Glory, 2021-12-15 15:18:08

What is the best way to initialize a class in Laravel so that its methods and variables are available everywhere?

We created a class that has an array - this is a list of error codes and messages, as well as a method that issues a message by the error code and returns it in the form
return response()->json(
.......
);

and how would it be better to do so that you can call this line in all controllers like
return $this->responser->getReturnData($ErrorCode);

You can override the constructor in each controller and start a class object there using new

$this->responser = new RectonderClass;

and then in each method refer to the class object and its method. But are there better methods?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
J
jazzus, 2021-12-15
@jazzus

Suddenly an absurd idea came up (the author will not like it), which already works great
return response()->json(trans('errors.500'), 500);

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question