Mersad Nilchy
Mersad Nilchy

Reputation: 612

Run this function only once when the website is opened and use it on all controllers Laravel

I want this function, which is connected to Crisp API, to run only once when the website is opened, and I use its in all controls. Because every time it wants to run, the application speed slows down.

      use Crisp;

      private function CrispClient()
        {
            $CrispClient = new Crisp();
    
            $CrispClient->authenticate("3a44293-706-4290-838-c063b58", "969b80676b5a671f2ee7a44f2452f7d152ed3c0c4a");

            return $CrispClient;
        }

Upvotes: 0

Views: 220

Answers (1)

Christophe Hubert
Christophe Hubert

Reputation: 2951

If you want to run the command once per request, I suggest using a Middleware to inject your $CrispClient in the request:

Middleware:

    public function handle($request, Closure $next)
    {
            $CrispClient = new Crisp();
            $CrispClient->authenticate("...", "...");

            $request->merge(['crisp_client' => $CrispClient]);
            return $next($request);
    }

Controller

$crispClient = request()->crisp_client;

Upvotes: 1

Related Questions