Php framework codeigniter: how to implement the mvc mode and a single portal _ PHP Tutorial

Source: Internet
Author: User
Php framework codeigniter implements the mvc mode and a single entry point. Here, I will not explain in detail what the mvc mode is. I just want to give a brief introduction. I can look for the specific information about MVC on the internet. the mvc mode breaks down a project into three parts, 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 CI official 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:

Reference content is as follows:
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 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. php controller name method name \ method parameter 1 parameter 2 of the method ......

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.

Here, I will not explain in detail what the MVC mode is. I just want to give a brief introduction. the specific information about mvc can be found on the network, in my understanding, the mvc pattern breaks down a project into three parts ,...

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.