An analysis of MVC programming and mvc programming in PHP programming. Analysis of MVC programming ideas in PHP programming, mvc programming ideas php mvc programming ideas have been widely used in the development of various large projects, many mature MVC frameworks have gradually been used to analyze MVC programming ideas and mvc programming ideas in PHP programming.
The MVC programming idea of PHP has been widely used in the development of various large projects. many mature MVC frameworks are gradually known and widely used in various projects, common examples include ThinkPHP, codeigniter, Symfony, yii, and cakePHP. This article briefly introduces the MVC programming idea of php.
1. what is MVC?
Simply put, the website source code is classified and layered.
The meanings of the three MVC letters:
M: Model, responsible for database operations.
V: View, which calls the Model to retrieve data and then calls the template to display the final effect.
C: the Controller, the program entry, decides which View to call and tells the View what to do.
In this case, the execution order of the program is C-V-M or C-M, and the MVC name is exactly the opposite.
II. why MVC?
1. make the physical structure of website programs more reasonable.
When using PHP to build a website, you may build a PHP file for each page in the most stupid way. If your website only has index. php, menu. php. article. three php pages, you don't need MVC, but when we do a general website, we usually have dozens of pages, and it is obviously not acceptable to put all the pages in the root directory, so you need a reasonable idea to classify your code, divide them into different directories by function, and load and call the code intelligently. this is what MVC will help you do.
2. make the code easier to maintain.
Let's take a look at a single page. the most stupid way is to mix PHP code with HTML code. this is obviously not good enough. you have to distinguish between PHP and HTML when maintaining your website, this is a disaster for a programmer. As a result, many people use Smarty to separate "data processing" from "page display". This is indeed good, and many people are doing this, but it is not MVC, MVC divides data processing into logical processing and database operations.
In this way, when your program is wrong or you want to modify it, it becomes easy. When an error is displayed on the page, you can check the V or template file; when there is a logic problem, you should check C and V; when your database operation is wrong, you should check M.
In fact, MVC generally divides a PHP page into four pages: C, V, M, and template. Perform their respective duties to facilitate management.
3. facilitates code reuse.
MVC usually puts a large function under a directory, that is, it is managed by a C.
For example, to create a website with a membership system, we can put all the code related to members in the user directory, which is managed by User_Controller in a unified manner. when another website also needs a membership system, we can directly copy the directory and modify the interface.
III. idea of implementing MVC in PHP
We need three base classes: Controller, View, and Model. different C, V, and M inherit from each other and they have corresponding attributes and methods. if you do not understand them here, you can read the object-oriented book.
Here we provide you with a design idea for the MVC base class for your reference only:
1.Controller class design
A main () method is called by the program. The get and post variables are used to determine how to handle the problem.
A getModel ($ model) method calls the M of the corresponding directory when you need to call the database.
A display ($ view) method, called in the main () method, loads the corresponding V, and drops the main () method of V;
2.The design of the View class is similar to that of the Controller.
A main () method. this method is called when C loads V so that the program can continue to run.
A getModel ($ model) method calls the M of the corresponding directory when you need to call the database.
A display ($ template) that calls the corresponding template file and passes the data to the template.
3.Model class design
You can define some attributes, such as tables to be operated and fields to be operated.
A getDB () method to obtain an instance of a database class (database classes are generally designed in single-piece mode)
Load () method to load a data.
An add () method automatically constructs an SQL statement based on the defined attributes and performs the insert operation.
An eidt () method, same as above, but is modified.
One del () method, the same as above, but the delete operation is performed.
To help new users better understand the working principles of my thinking, we now simulate a user login scenario to see how MVC works.
Now assume that all data is submitted to index. php,
Step 1:
We submit various get variables to tell index. php which C should be used, for example, index. php? Controller = user
Then index receives the get variable and does not need to do anything. you can directly find/user/controller. php, throwing all the data to him. Originally, GET and POST are global, so index. php does not need to do anything. you can directly call the main function of C. php task completed.
Step 2:
C's main function starts execution, checks the variables, and finds the login operation to be performed by the user (very simple, you can post the variable do = login), so call getModel, load the corresponding M class (for example,/user/models/model. php), and instantiate, call the load method of the instance, load the user's data, determine whether the password is consistent with the user submitted, if the submitted data is incorrect header jump to the error page, if it is correct, call the display () method to load the corresponding V (for example,/user/views/details. php), and instantiate it, call its main () function, and enter the third step. The task C has been completed, and the second operation is not performed in the main function.
Step 3:
You can choose to call getModel () to load M, rewrite and retrieve data, or pass the parameter (for example, SESSION) when C instantiates V. After V has determined the data, display (), load the template, and MVC execution is complete.
Of course, due to the limited words and energy, the content here is a very brief summary. many details should be taken into account during actual implementation. However, when designing MVC, the general idea is like this, in practice, I feel good.
What are the important website programming ideas of php?
First, you need to understand what MVC is used. MVC is used for cooperative development. for example, you have a development team. You are the director. you need to use an architecture to collaborate and develop your members in parallel. But when you are fighting on your own, the framework like MVC is really unnecessary. Instead, it is inefficient to rely too much on MVC to make metaphysical things.
For social networking websites, mvc cannot solve your problems. even if you don't need to be object-oriented, all pages use process storage, which is more efficient.
The same is true for object orientation. when you need multiple instances, you can create a class to facilitate operations. when you only need to be an object, the object-oriented approach is meaningless.
Don't think about the framework, mvc or something at the beginning of development. use the most direct method to make the first version as soon as possible, and then make improvements on demand first, this is the most appropriate method.
Php mvc mode
Mvc is just a programming idea, regardless of language.
Many php projects use mvc, which is suitable for large-scale project development.
MVC is short for three words: Model, View, and Controller ). The purpose of the MVC model is to achieve the functional division of the Web system. The Model layer implements the business logic in the system, which can usually be implemented using a JavaBean or EJB. The View layer is used for interaction with users. it is usually implemented using JSP. The Controller layer serves as a bridge between Model and View. it can dispatch user requests and select an appropriate View for display, it can also interpret user input and map them to operations that can be performed at the model layer.
The idea of MVC programming in PHP has been widely used in the development of various large projects, and many mature MVC frameworks have been gradually developed...