"Go" MVC schema

Source: Internet
Author: User
Tags php foreach

Although the MVC architecture has been burning for years. But I have never had the chance to really feel the essence of it, take the opportunity now, to turn an MVC architecture of the Beginner chapter

The controller (books_controller.php) receives the user's request in the form of an HTTP GET or post (we can also have a host controller, such as index.php, to receive the request, and then call ooks_controller.php).

The controller examines the request and the corresponding parameters, and then calls the model (book_model.php), requesting the model to return to the Fantasy class book. The model is responsible for fetching the information from the database, if necessary it can add filtering and logic, and then return to the book list. The controller uses the appropriate view to present the data to the user, and if the user also chooses a specific skin, the corresponding views file will be selected and so on.

What are the benefits of MVC?

One of the most obvious benefits of using MVC is that it separates the view presentation from the application logic clearly. Support for different users and different device types has been a common problem for the moment. A request from a desktop computer and a mobile phone should have a different view. The model returns exactly the same data, but the difference is that the controller chooses which view file to use to present the data (we can think of it as a different template). Apart from separating the view from the business logic, the separation of MVC also reduces the difficulty of large-scale application design. The code is also more structured and therefore easier to maintain, test, and reuse.

But why frame?

When you use a framework, the basic structure of MVC is already included, and you only need to augment these structures to place your files in the appropriate directory in the MVC pattern. It also offers a number of ready-to-test and fully tested functions. Take the example of CakePHP's most MVC framework. Once you've installed it, you'll find it contains three main directories:

* app/

* cake/

* vendors/

The app folder is where you place your application files. This is where you open the app. The cake folder is the location where cakephp files are stored and where it is developed (the important function of the framework is inside) The Vendors folder is the location where the required third-party PHP library files are stored. Your working folder (app folder) has the following structure:

* app/

config/

controllers/

locale/

models/

plugins/

tests/

tmp/

vendors/

o views/

o webroot/

Now you need to put your controller in the controllers directory, your model is placed in the models directory, and the view file is in the views directory. Once you're familiar with the framework, you'll know where to start when you need to modify and create the code. This kind of file organization way makes maintenance easy on a lot.

Take the framework as an example

Because this tutorial is not intended to teach you how to use cakephp to make an application, we just use it to show the demo code of the model, view, and controller, and make a comment on the benefits of using the MVC framework. These codes are simplified and are not suitable for practical applications.

Controller

Using the cakephp framework, our controller looks like this:

<?php

Class Bookscontroller extends AppController {

Function List ($category) {

$this->set (' books ', $this->book->findallbycategory ($category));

}

function Add () {...}

function Delete () {...}

... ... } ?>

It's simple, isn't it. This controller will be saved as books_controller.php and placed in the/app/controllers directory. It contains a list function to perform the actions in our example, and it can also contain other books-related actions (like adding new books, deleting new books)

The framework gives us a lot of content, listing all books in just one line of code. We have a base class defined in the basic controller behavior and we have inherited it. (AppController inherits from Controller) in the list operation, what we need to do is to call the model to get the data, and then select the appropriate view file to present the data to the user, let us explain the process. This->book is our model, and here's the code:

$this->book->findallbycategory ($category)

is to tell the model to return the list of books under the selected category, the following set method:

$this->set (' books ', $this->book->findallbycategory ($category));

is to tell the controller to transfer the data to the view. It transmits the data returned by the model to the view with a variable of books, and then the variable is accessible in the view. Now we need to render the view, and if you are using the default view, CakePHP will help you do it automatically. If you need to use a different view, you need to use the Render method to specifically declare:

Model

The model is very simple.

<?php

Class Book extends Appmodel {

}

?>

Why is it empty? Because it inherits some of the necessary functionality from the underlying class, we just need to follow CakePHP's naming conventions and the framework will automatically help you with other things. Like what. By name, CakePHP knows that this is the model used in Bookscontroller. It then automatically accesses a data table called books in the database. This definition, our book model has the ability to read, delete, and save data from the database. This code should be saved as books.php and placed under the/app/models file.

View

What we need to do now is to create a view (at least one) for the list operation. This view will use HTML code and also contain some PHP code to traverse the books array provided by the model.

<table> <tr> <th>Title</th> <th>Author</th> <th>Price</th> </tr >

<?php foreach ($books as $book):?> <tr> <td> <?php echo $book [' book ' [' title '];

?> </td> <td> <?php echo $book [' book '] [' author '];

?> </td> <td> <?php echo $book [' book '] [' price '];

?> </td> </tr> <?php Endforeach;?>

</table>

As we have seen, the view file does not generate a finished page, it is just a piece of HTML code. This is because cakephp provides a way to define the layout of the page, and the view is inserted into the layout. When creating these HTML fragments, the framework also provides helper objects to help us do common tasks (inserting forms, links, Ajax, or JavaScript), We save this default view as LIST.CTP (list is the name of the action, and the CTP refers to the cake template), and then place it under/app/views/books (in the books directory because it is the operation of the book Controller), With the help of cakephp, these three parts have been completed.

Conclusion

We have learned about the most commonly used architectural pattern MVC today, and we need to be aware that when we refer to the schema in the programming world, we are referring to a flexible architecture that can be used to solve the problems in the hands. We will find that the actual use will bring changes to the structure we see. But most importantly, this model will help us to differentiate the functions of each part of the program, facilitate program maintenance, code reuse, and testing. We've seen the benefits of using the MVC framework, which provides us with a basic MVC skeleton, as well as many useful features that improve our efficiency and make the development process easier.

"Transfer from http://www.uml.org.cn/sjms/201202061.asp"

"Go" MVC schema

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.