How MVC works with MVC models, views, controls, and their single-entry files

Source: Internet
Author: User
Tags php framework yii

What is Mvc,mvc is the Model view control, the model is responsible for database-related operations in the project, the view is views, responsible for the display of the page and data display, control is the controller, responsible for the middle of the logical transformation, data reception, output processing. The benefit of MVC is that project development allows the code and page to be broken down as much as possible, with as few associations as possible. This is just a way of development, it is said to come from Java, really deceptive Java AH.

The common PHP framework is basically the development model of MVC, for example: Yii (Praise), CI,TP (domestic), these are I have used, there are a lot of PHP framework too much, not one by one cases, the development model is similar, here I like the framework of Yii, Because this frame design is too powerful, is worthy of the full stack framework, like CI and TP is the general framework, the function is not much. There is no bright spot, hehe.

What is the development process, usually under the project directory, there is a folder called Controller, which is written in the control, and his (general) in the Category view of the table of contents is the page, there is a model of the directory, with database tables related to the data table named model file, Mainly in the controller inside the write function, and then use the model file provided in the method of the database is added and censored. View, is usually directly inside the controller call, Yii is $this->render (), CI is $this->load->view (), TP is $this->display (); These methods are basically outputting the data directly and then echo it out. Or add a third parameter to get the buffer content through Ob_start () ob_get_contents (), and then handle it according to the specific requirements.

What is a single entry (index.php)

Single entry is generally just a file index.php exposed, to access the relevant content through this file, how does this mode work? In fact, through the index.php, the core file of the system is loaded into, and then through the core file inside the routing class, the content of the request parsing, parsing out to access the Controller class and its methods. It doesn't matter if you can't read it directly;

When we visit deme/index.php/welcome/index

Take CI as an example:

First to expose such a directory, to be able to access to index.php, this time the index,php file to start execution, open the file we followed the code to define the environment constants first,

Then define some project directory constants to prepare for the subsequent loading of the file, and then skip the section to the bottom of the index.php

This is where the real work begins to introduce the system core file.

We open this file to see exactly what is written, open it still is to introduce a variety of constants, configuration files, continue to go we see a line of this

What is it? Let's print it out to see if this is the system core routing class.

Keep looking down. We see a property called the $rtr->class class that prints out what we see,

See two strings, one is welcome, one is Index,welcome is the default to access the controller, index is the name of the method we want to access. Well, this time really started to start, this time to determine whether the file exists, if there is introduced, does not exist on the 404, and then $c = new Controller ();

First load the class to be accessed, then instantiate one, and then call Call_user_func_array this function, execute the method of instantiating the object, is equivalent to $welcome = new welcome ();

Then execute $welcome->index ();

OK the whole process is probably this way, if do not understand can see a few more eyes, experience, can understand, have the opportunity I also write a simple MVC, single-entry file framework, so that can make people understand this thing more clearly.

How MVC works with MVC models, views, controls, and their single-entry files

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.