Php-mvc news project experience-1

Source: Internet
Author: User
Tags define function
Php-mvc news project experience-1 I have been studying the custom mvc of pengcheng and its news instances over the past few days to learn about the mvc power. Once again, I would like to express my high respect for pengcheng ~ Although the framework is full, it does not prevent us from deepening our understanding of the framework by thinking about our own framework, so we must know more about it. First, the pengcheng is fragrant to the great gods... bow.

Okay, don't bother ~ Go to the topic.

The previous three sections write a simple MVC structure and the introduction of Smarty. In MVC, we can use Smarty to complete its functions. in M, we simply return a piece of data, and in C, we only implement automatic instantiation of various types. M and C are never that simple. Now let's use a [news instance] to learn the advantages of controller and model.

Originally, documents of various functions need to be classified in a standardized manner. For convenience, we can avoid the problem of tossing the file path. The file layout is as follows:

Among them, the data file includes the template files defined by Smarty. The smarty folder stores the downloaded Smarty. it looks messy ~

Config. php contains the configuration information of each parameter (VIEW engine and DB engine), packaged into an array

Encapsulate VIEW engine

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

This VIEW class defines three methods:

The init method, as its name implies, is used to initialize Smarty, including introducing Smarty. class. php file, generate the smarty class through the passed $ viewtype, and assign values to each property of the Smarty through the passed $ config parameter (array.

The assign method is defined here to wrap it into a common static method for external calls (no need to instantiate it). after The assign method is transformed, you can package the data into an array as a parameter to input each value in the newly packaged assign registration array.

The display method is used to wrap it into a common static method.

Finally, let's review the methods of this VIEW class. they are all public and static, that is, we do not need to import smarty for external calls in the future. class. php and instantiation, directly VIEW: assign () registration, and then VIEW: display () can simply implement the view function in mvc ~

Encapsulate database engines

Because our project needs to retain the data, we need to use the database for addition, deletion, modification, and query. We also thought, can we encapsulate a database engine and call the static method directly for database operations in the future?

We define a static attribute in the same class to store the instantiated object. Define static methods to facilitate external calls.

I think this method is like a java abstract class: we can define attributes and abstract methods in it, without specific implementation, but only need to develop functions. But it is a little different from the abstract class: external calls do not use new, and there is no inheritance. the specific implementation of a method depends on an Init method new class (I call it an implementation class ). You can use your own static attributes to store the 'implementation class' method, and call the class through your own methods to implement the specific method of 'implementation class.

The encapsulated engine is like a second-hand car ~ Define a static variable to accept the various old vehicles (methods) of the 'implementation class', and then sell the old cars (methods) to a brand new shell (packaged as a common static method public static.

The above VIEW engine has Smarty as the 'implementation class', but the database (DB) engine does not, so we will define its 'implementation class' to use the current method. Is mysql. class. php. As for the mysql class encapsulation, the benevolent sees the wise and wise, how can we encapsulate it easily?

Note: 1> there cannot be output statements in the engine. the MVC idea is the division of labor and layers. Therefore, after data is obtained, data cannot be echo, but can only be returned.

2> The DB engine does not represent a Model. it is only a part of the Model and can be understood as a Model tool.

Here, in MVC, The view layer is done, and the model layer is coming to an end.

In the previous sections, we have learned that the controller function is to instantiate views and models and call their methods. However, instead of directly instantiating the controller, we define function. php to instantiate the function. Function. php is already an old acquaintance, as shown in the figure below:

Some friends may wonder: Aren't you kidding me? Good MVC, good V method?

Don't worry, I don't know a monkey. The V method has been implemented in Smarty. initializing Smarty is to initialize the View. The VIEW engine replaces V's work.

Encapsulate startup engine

I have repeatedly stressed that this project is the magic pen of pengcheng, and the startup engine is named PC. php. Imports files, instantiates engines, and initializes url transmission parameters.

Encapsulate a public method run and execute each private method to complete initialization to improve security.

Next, we need to encapsulate a unified entry file and directly call the run method in it.

Test:

Sorry, I forgot to define the controller file. it's also funny. I admit that I was sent by a monkey ~ Haha

Well, the functions of the controller are highlighted here. The process is like this, from the unified portal file admin. php comes in, call run to initialize each engine method, and then use the C method to enter the control layer. call the url to pass the parameter controller = xxx & method = xxx corresponding to M and V to complete the business. Define an adminController. class. php file.


Write only one test method to test the call of the test method:

If the test is successful, the entry file, control layer, and view layer are all correct. You can proceed with development.

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.