Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

tbergman001's avatar

Code Block

Hello All,

I have a question regarding the use of the same 9 lines of code throughout many controller functions and the best practice for this with regards to Laravel.

We are building out an internal tool which interacts with Microsoft Navision and to consume some of its data through ODATA I use the same 9 lines of code repeatedly with only preset variables which change.

        $login = $this->odataLogin;
                $password = $this->odataPass;
                $url = $this->odataBaseURL."Customer_List?$format=json&$filter=No%20eq%20'$nav_cNumber'";
                $ch = curl_init();
                curl_setopt($ch, CURLOPT_URL,$url);
                curl_setopt($ch, CURLOPT_FRESH_CONNECT,$url);
                curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
                curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
                curl_setopt($ch, CURLOPT_USERPWD, "$login:$password");
                $result = curl_exec($ch);
                curl_close($ch);  

                $obj = new \stdClass();

                $obj = json_decode($result, true);

Is there a way to call this into multiple controllers and functions more effectively?

Thanks,

0 likes
2 replies

Please or to participate in this conversation.