How does the PHP framework codeigniter implement the mvc mode and single entry_PHP tutorial

WBOY
Release: 2016-07-13 17:40:04
Original
1018 people have browsed it

About mvc

I won’t explain in detail what the MVC pattern is here, just a brief introduction. Specific information about MVC can be found on the Internet. As I understand it, the MVC pattern decomposes a project into three parts, namely Model, View, Controller, the abbreviation combination of these three words is MVC. MVC is a common software agile development model, used in many It has been widely used in the field, especially in the field of desktop programming. However, it is difficult to implement in scripting languages ​​​​like PHP. Especially a few years ago, it was 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 each framework. Let’s not mention the implementation methods in other frameworks. Here we only introduce how codeigniter implements mvc.

About single entrance

Single entry means that in a website (application), all requests are directed to a script file, such as http:localhostindex.php in CI, so All access to the application must go through this entrance. It is the single entrance that enables the MVC mode to be realized, because when you access index.php, the application will do a lot of initialization work. To do this, call a large number of basic class libraries, load the controller according to the parameters behind index.php, and then load content information such as views and models.

All file loading in CI must be called by the controller. Because the controller is the super class in CI, that is, other classes are attached to it, so when accessing the CI application using a single entry method, you need to After index.php, add the name of the controller and the method name in the controller. If you have no idea about this or cannot understand it, you can go to the official website of CI to download its official documentation and learn more about it. How it works

CI’s official documentation is very detailed and easy to understand. What is described here is the basic principles that do not exist in the documentation.

Start

Perhaps we should first explain how the CI controller works. A controller in CI is a class written by the user. It inherits from the system’s Controller class. For example, suppose we want to build a This can be accessed via http:localhostindex.phpcontrolfuncparam1param2 What do we need to do on the page we asked about? First we need to create a new file in the systemapplicationcontrollers folder. contro.php file, this file is the file where the controller class we want to access is located. Create the following content 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 }

This is not the 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, which is the third parameter after the url. Two parts. 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 of the single entrance is actually: http:localhostindex.phpController name method name method parameter 1 method parameter 2 . .. ..

Each method in the controller class represents a page, that is, many similar operations can be put into a controller to unify the operations

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

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/486250.htmlTechArticleAbout mvc This is not a detailed explanation of what the MVC pattern is, just a brief introduction. For specific information about mvc, you can go to the Internet Looking for it, in my understanding, the MVC mode breaks a project into three parts,...
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!