PHP-MVC News Project Experience-1

Source: Internet
Author: User
Tags define abstract

These days have been delving into Pengcheng's custom MVC, and his news stories, to learn about the power of MVC. Once again, I salute the great god of Pengcheng-the framework is full of streets, but it does not prevent us from conceiving our own framework to deepen our understanding of the framework, knowing its reasons. First to Pengcheng the direction of the great God incense .... Take a bow.

OK, no trouble ~ get to the chase.

The last 3 sections write the simple MVC structure and the introduction of Smarty. MVC in V We can use Smarty to complete its function, in addition m inside we simply return a piece of data, C inside we just realize automatic instantiation of all kinds. M and C will never be so simple. Now let's get a "news example" of the controller and model.

Originally, the documents of each function need to be classified in a normative way. For convenience, avoid tossing file path problems. The file layout is as follows:

Where the data file includes the individual template files defined by Smarty, the Smarty folder is the smarty that holds the download. Looks a bit messy, master don't spray ~

config.php contains configuration information for each parameter (view engine and DB engine), packaged as an array

Package View engine

First, to implement the view function, define a view engine: view.php

There are three methods defined within this view class:

Method Init, as the name implies, is to complete the initialization of the Smarty: including the introduction of the Smarty.class.php file, the Smarty class is generated through the incoming $viewtype, and the $config properties are assigned values through the incoming smarty parameter (array).

Method Assign, here again defines the Assign method, is to wrap it into a common static method, convenient for external invocation (no further instantiation), and assign method through the transformation, The data can be packaged in the form of an array as a parameter to each value in the new wrapper's assign register.

Method display, here just to wrap it up as a common static method.

Finally, let's revisit the method of the view class, which are public static, that is to say, We no longer need to import smarty.class.php and instantiation when we call outside, View::assign () register directly, and then View::display () can simply implement the view function in MVC ~

encapsulating the Database engine

As our project needs to keep the data, we need to use the database to change the additions and deletions. We also think, can you encapsulate a database engine, and then call the static method directly when you need to do the database operation OK?

We're like this. Defines a static property in the class that stores the object after the instantiation. Define individual static methods to facilitate direct external invocation.

I think this approach is like Java's abstract class: We can define attributes inside, define abstract methods, do not need to be implemented specifically, only need to develop functions. But there are some small differences with abstract classes: External calls do not have new, there is no inheritance, and the concrete implementation of the method relies on an Init method, the new class (which I call the implementation class). The method of storing the ' implementation class ' by its own static property, and invoking the class through its own method to implement the concrete method of implementing the class.

Figuratively speaking: The encapsulated engine is like selling a used car-define a static variable to accept the ' implementation class ' of the various old cars (methods), and then replace the old car (method) with a new shell (packaged into common static method public static) for sale.

The view engine above has smarty as the ' implementation class ', but the database (DB) engine does not, then we define its ' implementation class ' To have the present method. is for mysql.class.php. As for the MySQL class package benevolent See, how convenient to package Bai

Note:1> engine inside can not have output statement, MVC idea is division of labor, so get data can not echo, can only return.

The 2>DB engine does not represent model, but is part of the model and can be understood as a tool of model

Here, in our MVC, the view layer is fixed and the model layer is temporarily over.

The controller functions have been learned in the previous sections to instantiate the view and model and invoke their methods. However, instead of instantiating directly in the controller, we instantiate it by defining the function function.php. Function.php is already an old acquaintance, see figure:

See here may have a friend will question: you this is not kidding me? Well, what about MVC's good V method?

Rest assured, I do not know the monkey, it also please not move elder brother. The V method has already been implemented in Smarty, and initializing the Smarty is the initialization of the view. The view engine replaces the work of V.

encapsulating the startup engine

I have repeatedly stressed that this project is the genius of the Pengcheng, and the starting engine is named pc.php by its name. Responsible for importing each file, completing the instantiation of each engine, and initializing the URL pass parameters.

This encapsulates a public method run unified execution of individual private methods to complete initialization, improve security

Next, we need to encapsulate a unified portal file that calls the Run method directly inside

Test it:

Sorry, forgot to define the controller file, is also funny, I admit I was sent by monkeys ~ haha

Well, here's a more prominent function of the controller, the process is this, from the unified import file admin.php come in, call run to initialize the various engine methods, and then through the C method, into the control layer, call URL parameter controller=xxx& Method=xxx the corresponding m and V to complete the business. So, define a adminController.class.php file


Just write a test method that tests the method called:

The test succeeds, stating that the entry file, the control layer, and the view layer are all fine. You can then develop the






















PHP-MVC News Project Experience-1

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.