Thinking about using curl in the frame, and curl is actually very useful

Source: Internet
Author: User

Preliminary conjecture:

In contact with the first stage of the framework document, you will feel that the controller call model is a very simple thing, TP use D method or M method to instantiate the model, Laravel with a namespace to load the model, CI with $this->load->model to load the model, It's very convenient, isn't it.

The previous article mentions a logical thinking about scheduling, as well as the idea of loading all the classes that might be used in the original design. The similarity is that the number of classes loaded is indeterminate, and only if your business logic is completely fixed will there be a certain number of classes that will be loaded.

So, does curl appear to be a way to change this? Of course, the use of PHP autoload can also achieve this effect, here only curl speculation, curl in my impression is used to imitate the client access link and get the return value of the function.

Observation:

Direct access to the methods in the model to get to the data, there is also a point to mention that the company project is a number of projects in the same framework, the model is not placed in a single project in the Models folder, but placed in the outermost (and project peer) service layer, In the words of Mr. Chen, it is the separation of logic and expression. This involves other aspects of the extension.

Well, first of all, the idea of layering logic and expression .

The rest becomes the use of the Curl tool.

Four Steps


1. Initializing Resources

$handle = Curl_init ();

2. Setting the parameters, which is the most important step in the legend, refer to the manual http://php.net/manual/zh/function.curl-setopt.php

Like accessing the model in a project, the returned data is not exported directly to the browser, so the approximate settings are as follows:

curl_setopt ($handle, Curlopt_url, $uri);
curl_setopt ($handle, Curlopt_returntransfer, 1);

Curlopt_url sets the URL address that needs to get the data, which can be passed as a parameter in Curl_init ().

Curlopt_returntransfer is set to 1, and the data is returned as a string.

3. Implementation

$rs =curl_exec ($handle);

4. Close

Curl_close ($handle)

At this point, the method of getting the data from instantiating the model object, and executing the method, becomes the method of directly accessing the model through a URL

Analysis:

One is the framework instantiation model, and then the various operations of the model.

One is to directly access the URL, simulating and fetching data through curl.

Guess:

Using the Curl tool to access the URL does not have to load the model, without instantiating it (and of course, in addition to itself), to some extent improve performance.

The key is not to manually load the unpredictable model, what data interface to use directly access.

So what does it do to separate the logic from the presentation layer by using the service layer alone ....

Thinking about using curl in the frame, and curl is actually very useful

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.