Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to understand MVC pattern and single entry in PHP Framework

2025-01-22 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

Today, I will talk to you about how to understand the MVC pattern and single entry in the PHP framework, which may not be well understood by many people. In order to make you understand better, the editor has summarized the following contents for you. I hope you can get something according to this article.

About MVC

MVC pattern is not explained in detail here, but just a brief introduction. Specific information about MVC can be found on the Internet. As far as I understand it, MVC pattern divides a project into three parts, namely, Model (model), View (view), and Controller (controller). The abbreviated combination of these three words is MVC.MVC, which is a common software agile development pattern. It has been widely used in many fields, especially in the field of desktop programming, but it is difficult to achieve in scripting languages like php, especially a few years ago, it is difficult to see the implementation of MVC in scripting languages, but this year, with the emergence of many frameworks, MVC has been initially implemented in various frameworks, and the implementation methods in other frameworks are not mentioned. Here is just an introduction to how codeigniter implements MVC.

About single entrance

A single entry means that in a website (application), all requests are directed to a script file, such as http:\\ localhost\ index.php in CI. All access to the application must go through this entry, and it is the single entry that makes the MVC pattern possible, because when you visit index.php, the application does a lot of initialization work and calls a large number of basic class libraries. And load the controller according to the parameters behind the index.php, and then load the diagram, model and other content information.

All the file loading of ci is called by the controller, because the controller is a superclass in CI, that is, other classes are attached to it, so when you access the CI application with a single entry, you need to add the controller name and the method name in the controller after the index.php. If you don't have any idea or do not understand this, you can go to CI's official website to download its official documentation. And then learn more about how it works.

The official documentation of CI is very detailed and easy to understand, describing 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 page that can be accessed through http:\\ localhost\ index.php\ control\ func\ param1\ param2. First, we need to create a new file contro.php file under the system\ application\ controllers\ folder. This file is the same as the controller class we are going to access, and create the following in this file:

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 an example of a controller that includes model and view

First of all, note that the class name of the controller should be capitalized, and then the constructor of the parent class should be called in the constructor of the class, followed by the func () method, the second part of the parameters followed by url, which takes two parameters, the values of which are the values of the third and fourth parts of url. That is, the access method for a single entry is actually: http:\\ localhost\ index.php\ controller name\ method name\ method parameter 1\ method parameter 2.

In the controller class, each method represents a page, that is, many similar operations can be put into a controller to achieve the unity of the operation.

In the above example, the other part of the func () method loads model and view, respectively. When loading model, it loads the MSomemodel class in the msomemodel.php file in the models folder, which is responsible for the model part of the application, that is, responsible for data exchange, such as database storage.

Then we execute a method in model through $data= $this- > MSomemodel- > getvalue (), and return the data from this method, and then assign a value to $data ['data1'], $data is an associative array through which we pass values to the view view file instead of using the usual template mode, this method better separates the processing of various parts of MVC and has its own unique side in performance.

Then we pass the $data array to the welcome.php file in the views folder, which is a regular php and html mixed script, in which you can use the passed $data array output information, but note that you don't need to use $data ['data1'] when outputting information in the view file, just echo $data1;.

This is the basic way to work. Let's analyze the implementation at the code level.

Code analysis

In CI, the Controller class is treated as a superclass, that is, all the processes of loading the MVC implementation pattern start from the Controller class, so we ignore the execution process before the CI is loaded into this class, and start the analysis directly from the file where the Controller class is located.

The file where the Controller class is located is in the system/libraries/Controller.php file.

In this class, all necessary base classes are loaded first, including the 'Config','Input',' Benchmark', 'URI',' Output', 'Language',' Router' class. The Roader class is then loaded and its _ ci_autoloader () method is executed, which is the core of the MVC pattern through which all other content in the controller is loaded. Let's analyze its code:

First of all, let's take a look at the _ ci_autoloader () method, which implements the automatic loading of certain class libraries or classes. If you always use certain classes in your application, but you are not sure whether these classes have been loaded automatically in CI, you can set the array of library or helper or plugin to be loaded automatically in the config/autoload.php file. Please refer to the manual for details.

First look at how CI loads libraries, which allows you to use $this- > load- > library ("name") anywhere in your controller (usually in the constructor). To load a class, which can be a user-defined class or a system class library. User-defined classes need to follow the conventions of CI. For details, see the manual "create your own class library". The library () method takes a string or an array of class library names as * * parameters, and the subsequent processing will traverse and then load all the classes. You can pass parameters to the constructor of the class to be loaded through the second parameter, and the third parameter allows you to define the name of the returned object. The last two parameters are not usually used. This method simply determines whether the parameter is empty or not and then calls the method _ ci_load_class ($class, $params = NULL, $object_name = NULL), which is a very complex function that loads the class specified by * parameters. After complex path determination in this class and finding the required class file, the method _ ci_init_class ($class,'', $params, $object_name) is called. This class is used to instantiate a class. If the third parameter above is included in the statement that loads the class, an instance is returned with this parameter as the instance name, and if the third parameter is not set, an instance name with the class name is returned, which is why the model class name is directly used as an object in the previous example after loading model.

Then let's take a look at how CI loads the model. This method allows you to load the model in the controller using $this- > load- > model ($modelname,$name,$db_conn). These three parameters are the name of the loaded model, the name of the object instantiated after loading, and whether to automatically connect to the database. The last two parameters can be omitted. You can say that multiple models can be loaded at once, and only need to set the * * parameters into an array. This method first decomposes the passed * * parameters into an array with "\". This mechanism allows you to create multi-tier folders in the model, arrange the code grouping more reasonably, and then the program takes an element of the array as the name of the class to be loaded. And look for this class according to the path, then include the file, and instantiate the class. If the second parameter is set, it will be instantiated to the object of $name, otherwise the class name will be instantiated by default.

Let's look at how CI loads the view. The * * parameter of the view ($view, $vars = array (), $return = FALSE) method is the name of the view to be loaded, the second parameter is the value of the variable to be passed to the view, and the third parameter specifies whether to return the data of the output buffer. This method calls the _ ci_load ($_ ci_data) method with all the arrays as an array parameter, which parses the passed array of variables into a symbol table through the extract () function (that is, treating the key name as the variable name) Values as the values of variables), and cache these variables so that they can communicate with each other in different views, that is, this method allows multiple calls. In order to automatically load variables that have been passed to the previous view at each call, all variables passed to the view are cached in a property of the class, so that all variables are obtained each time the method is called. The view file is then loaded and assigned to the global variable $OUT as part of the output buffer, which is used to control the buffer output, which improves efficiency and makes the debugging time more accurate.

The principle of other loading methods is basically the same as that of the above method, but it changes a little depending on the situation. CI includes all the files in the controller in the method of implementing the MVC pattern. After we include these files, we are free to use these objects and data in the controller, and then * output all the data by buffering the output class, although the structure of the Loader class looks very complex. But in fact, its implementation is very simple, its internal code principle is basically the same, and clear, it is not difficult to understand carefully.

After reading the above, do you have any further understanding of how to understand the MVC pattern and single entry in the PHP framework? If you want to know more knowledge or related content, please follow the industry information channel, thank you for your support.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report