About MVC
Here, I will not explain in detail what the MVC mode is. I will just give a brief introduction. The specific information about MVC can be found on the Internet. The MVC mode breaks down a project into three parts in my understanding, model, View, and Controller are the abbreviations and combinations of the three words: MVC. MVC is a common agile software development model. It has been widely used in many fields, especially in the desktop programming field. However, it is difficult to implement MVC in script languages like php, especially a few years ago, it was difficult to see the implementation of MVC in the scripting language. However, with the emergence of many frameworks this year, MVC has been initially implemented in various frameworks, here we will only introduce how codeigniter implements MVC.
About a single portal
A single portal refers to a website (Application) where all requests point to a script file, for example, http: \ localhostindex in CI. php, All accesses to the application must go through this portal. It is just a single inspector that enables the MVC mode to be implemented, because when you access the index. in php, the application will perform a lot of initialization work, call a lot of basic class libraries, and according to the index. the parameters after php load the controller, and then load the attempt, model and other content information.
All ci files must be called by the Controller. Because the controller is a super class in CI, that is, other classes are attached to it, when accessing the CI application using a single portal, in index. php is followed by the Controller name and the method name in the Controller. If you have no idea or cannot understand this, you can download its official documentation from the official CI website, then, learn more about how it works.
The official CI documentation is very detailed and easy to understand. Here we describe the basic principles that do not exist in the document.
Start
Maybe we should first explain how the CI Controller works. A Controller in CI is a class written by the user, which inherits from the Controller class of the system, for example, suppose we want to build a host that can pass http: \ localhostindex. what do we need to do when phpcontrolfuncparam1param2 accesses the page? First, we need to create a file contro in the systemapplicationcontrollers folder. PHP file. This file is the file where the Controller class we want to access is located. In this file, create the following content:
1 class Controller extends Controller { 2 3 function Controller() 4 { 5 parent::Controller(); 6 } 7 8 function func($param1,$param2) 9 { 10 $this->load->model('MSomemodel','',TRUE); 11 $data['data1']= $this->MSomemodel->getvalue(); 12 $this->load->view('welcome',$data); 13 } 14 } 15 |
This is not a basic component of a controller, but a controller example that contains the model and view,
First, note that the class name of the Controller should be capitalized, and then the constructor of the parent class should be called in the class constructor, followed by the func () method, that is, the second part of the parameter after the url. This method has two parameters. The values of these two parameters are the values of the third and fourth parts of the url, that is, the access method for a single entry is actually: http: \ localhostindex. parameter 1 of the php controller namemethod method parameter 2 ......
In the Controller class, each method represents a page, that is, many similar operations can be put in a controller to unify the operations.
In the above example, the model and view are loaded in other parts of the func () method, and the model is loaded in the msomemodel in the models folder. the MSomemodel class in the PHP file. This class is responsible for the model part of the application, that is, for data exchange, such as database storage.
Then, we use $ data = $ this-> MSomemodel-> getvalue () to execute a method in the model and return data from this method, then assign the value to $ data ['data1']. $ data is an associated array. We use this array to pass values to the view File, instead of using common template modes, this method better separates the processing of various MVC parts and has a unique performance aspect.
Then we pass the $ data array to welcome in the views folder. php file. This file is a common php and html mixed-write script. In this script, you can use the $ data array to output information, however, you do not need to use $ data ['data1'] to output information in the view File, but only need echo $ data1.
This is the basic way of working. The following code-level analysis and implementation
Code Analysis
In CI, the Controller class is processed as a superclass, that is, all processes loading the MVC implementation mode Start from the Controller class, therefore, we ignore the execution process before the CI class is loaded to this class, and analyze it directly from the file where the Controller class is located.
The file of the Controller class is located in the system/libraries/Controller. php file.
This class first loads all necessary basic classes, including: 'config', 'input', 'benchmark', 'uri', 'output', 'language ', 'router 'class. then the Roader class is loaded and Its _ ci_autoloader () method is executed. This class is the core of the MVC mode. It is used to load all other content in the controller, the following code is analyzed:
First, let's take a look at the _ ci_autoloader () method. This function automatically loads some class libraries or classes. If some classes are always used in your application, however, if you do not ensure that these classes are automatically loaded in CI, you can use config/autoload. set the library, helper, or plugin array to be automatically loaded in the PHP file. for more information, see the manual.
First, let's take a look at how CI loads libraries. This method allows you to be anywhere in your controller (usually in constructors) use $ this-> load-> library ("name"); to load a class. this class can be a user-defined class or a system class library, custom classes must follow the CI conventions. For more information, see the section "create your own class library" in the manual. the library () method uses a string or an array of class library names as the first parameter. The subsequent processing will traverse and load all classes, you can use the second parameter to pass parameters to the constructor of the class to be loaded. The third parameter allows you to define the name of the returned object. The two parameters below are usually not used, this method calls the _ ci_load_class ($ class, $ params = NULL, $ object_name = NULL) method after determining whether the parameter is NULL. This is a very complex function, this class loads the class specified by the first parameter. After complicated path judgment is performed in this class, the required class file is found, and the method _ ci_init_class ($ class, '', $ params, $ object_name); this class is used to instantiate a class. If the statement for loading this class contains the preceding third parameter, an instance is returned, this parameter is used as the Instance name. If the third parameter is not set, an Instance name named after the class name is returned. This is why the model is loaded in the previous example, the reason for using the model class name as an object directly.
Then let's take a look at how CI loads the model. this method allows you to use $ this-> load-> model ($ modelname, $ name, $ db_conn) in the Controller to load the model, these three parameters are respectively the name of the loaded model, the name of the instantiated object after loading, and whether to automatically connect to the database. the following two parameters can be omitted. you can load multiple models at a time. You only need to set the first parameter to an array, this method first splits the first passed parameter into an array. This mechanism allows you to create multi-layer folders in the model and arrange the code grouping more rationally, the program then extracts the last element of the array as the name of the class to be loaded, finds this class based on the path, then contains this file, and instantiates this class. If the second parameter is set, it is instantiated into the $ name object. Otherwise, the class name is instantiated as the object name by default.
Let's take a look at how CI loads a view. The first parameter of the view ($ view, $ vars = array (), $ return = FALSE) method is the name of the view to be loaded, the second parameter is the variable value to be passed to the view, and the third parameter specifies whether to return data in the output buffer. this method calls the _ ci_load ($ _ ci_data) method as an array parameter. This method passes the passed variable array through extract () the function is parsed into a symbol table (that is, the key name is treated as the variable name, and the value is used as the variable value), and these variables are cached, in this way, variables can be exchanged in different views, that is, this method can be called multiple times. In order to automatically load the variables that have been passed to the previous view during each call, all the variables passed to the view are cached in a property of the class, so that all the variables are obtained each time a method is called. then load the View File and assign it to the global variable $ OUT as part of the output buffer. This variable is used to control the buffer output, this improves the efficiency and the debugging time.
The other loading methods are basically the same as the above method, but a little changed according to the situation. CI includes all the files in the Controller in the MVC mode, after we include these files, we can freely use these objects and data in the controller, and finally output all the data by buffering the output class, although the structure of the Loader class looks complicated, its implementation is actually very simple. Its internal code principles are basically the same, and it is clear and clear. It is not hard to understand it if you look at it carefully.