This article analyzes the CodeIgniter configuration autoload.php automatic loading usage. Share it with everyone for your reference, the details are as follows:
CodeIgniter has an automatic loading function, which can globally load class libraries, models, configurations, language packages, etc., which is very convenient for functions that need to be used globally.
For example: There is a global function written in app_helper.php. If you need to load this function globally, you only need to set autoload.php:
Copy code The code is as follows: $autoload['helper'] = array('app');
Next, all places can be used, with similar configurations, models, etc. But while it is convenient, you also need to consider the disadvantages of this loading method.
If a project is divided into two parts, such as the frontend and the backend, is this function required for both the frontend and the backend? If there are different business modules for the front and backend, does each module need to be used?
If it is necessary, then it is good to write it here. If it is not needed, it is not recommended to write it here.
Relevant class libraries and function calls should be loaded on demand
There are many ways to implement loading. You can load on a specified page, you can load in a public controller or function, and you can use it globally once loaded. My common practice is to ignore the file and manually load global functions, etc.
Speaking of which, by the way, let’s talk about the loading mechanism of CI. The following is the loading method of class libraries, functions, etc.:
$this->load->library('session'); $this->load->model('hello_model'); $this->load->helper(array('url', 'array')); $this->load->language(array('user_menu', 'user_tips'));
The loading method is unified and relatively simple to use, but it is a bit inconvenient to pass parameters when loading the class library. When loading the class library again, it will not be loaded again, but will be taken out from the saved static array. You need to pay attention to the status of the member attributes to prevent program exceptions because the values still exist.
Readers who are interested in more CodeIgniter related content can check out the special topics on this site: "codeigniter introductory tutorial" and "CI (CodeIgniter) framework advanced tutorial"
I hope this article will be helpful to everyone’s PHP program design based on the CodeIgniter framework.