E
E
Eugene2021-05-17 11:18:17
Laravel
Eugene, 2021-05-17 11:18:17

How to register a global variable or get data via API once when loading a page?

There is a method that (for example, checkContract() ) calls the API to another server and requests the status of the contract (open or closed).
Now it is implemented in such a way that through the helper in BLADE, and in the methods of the controllers themselves, there is a constant call to this checkContract() method to check the status status.

And it turns out that when accessing this, the system each time knocks on the API. Those. when loading one page, it can request a contract several times (a couple of times in the blade template), and three times in different methods of different controllers.

Prompt how correctly to implement in such cases. At what point do you need to write the checkContract() method, and write it to a global variable accessible to everyone.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
V
vism, 2021-05-17
@vism

1. cache
2. singleton
3. stupidly static variable in the class.

J
James026, 2021-05-19
@James026

1 and 2 are the most normal, but about the second - a regular class that is registered in the container as a singleton

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question