Introduction to using MVC in PHP (2)

Source: Internet
Author: User
Tags php session php error smarty template

(Walter. Fan compiled an introduction to MVC using PHP written by Jason E. Sweat)

Here is a simple MVC application example:
In this example, we use the phrame framework.
Implement the MVC mode. phrame is a PHP implementation solution of Jakarta Struts, and its control file is not like struts
This is an XML file, but the most common array in PHP.

In phrame, like struts, the action, forms and forwards classes are used.
Loosely coupled models and views.
Each time the controller is initialized, it creates an instance that extends its subclass from the action class.
Controller will call the process () method of your action subclass,
A forwards list and a form object containing all HTTP request Variables

The expected result of your action is to determine the appropriate forward Object and return it to the Controller.
The Controller will process this forward Object and exit the process for this request.

The role of an action object. It can be used as an analogy between an HTTP request and a sentence.
You can think that in this sentence, action is a verb, model is a noun, and view is an adjective.
In other words, a specific web request will execute an action (verb) on a model (noun ),
Or display a view to describe (adjective) A model (noun)

This analogy describes the relationship between action and model, which can be considered when selecting an object name.

This example is a modified version of the phrame Hello world example (http://phrame.itsd.ttu.edu /).
Source code can be downloaded from http://sourceforge.net/projects/phrame
In this example, I removed some PHP warnings and used smarty instead of XSLT to format the view.

The MVC application uses a bootstap file, which is a separate PHP file and the core of the application.
In this example, the bootstap file is hello. php. Let's take a look at this bootstap file.
<?
Error_reporting (e_all );
Define ('phrame _ lib_path ','.../../phrame /');
Require_once phrame_lib_path. 'include. Jes. php ';
Require_once 'smarty. Class. php ';
Require_once 'ingingmanager. php ';
Require_once 'actions/helloaction. php ';
Require_once 'models/person. php ';
Require_once 'models/helloerrors. php ';
Define ('appl _ view', 'Hello. php? '. _ View.' = ');
Define ('appl _ ACTN ', 'Hello. php ');
?>
In a phrame program, the relationship between forms, actions and forwards is
Created in the traditional phrame option array. I think this array is monotonous and lengthy, which is not conducive to development and maintenance.
A mappingmanager class is created to manage and maintain these relationships.
This class can be extended by any phrame application, and it has methods to return
PHP array used by phrame.
The benefit of this class is that when you add them, you can verify the relationship between form and action mapping.
(This avoids errors when you try to use the phrame option array)
Class hellomap extends mappingmanager
{
Function hellomap ()
{
// Set options
$ This-> _ setoptions ('handle _ error ');
// Add Application Forms
$ This-> _ addform ('helloform', 'actionform ');
// Add application actions and forwards
$ This-> _ addmapping ('sayhel', 'helloaction', appl_view. 'index', 'helloform ');
$ This-> _ addforward ('sayhel', 'index ');
$ This-> _ addforward ('sayhello', 'Hello', appl_view. 'hello ');
}
}

In the hellomap class constructor, we have completed what we need to do.
First, reload the traditional phrame option array and we define an error handling function.
Next, let's look at the form to be recognized. In this example, the standard phrame actionform class does not need to be extended.
Finally, define actions and forwards. In this example, there is only one action: sayhello
Two forwards: "Index" and "hello"
The parameter of the mappingmanager: _ addmapping () method is
(Mapping name, implementation of this mapping class, call the default address of action, the form associated with the action in mapping)

The parameter of the mappingmanager: _ addforward () method is
Identifier of Action mapping, the identifier of forward, and the optional Redirection URL (if no action is associated with it)

<?
Session_start ();
$ Smarty = & New smarty;
$ Map = & New hellomap;
$ Controller = & New actioncontroller ($ map-> getoptions ());
$ Errors = & New helloerrors;
Function handle_error ($ number, $ message, $ file, $ line, $ context)
{
Appl_error ($ message );
}
Function appl_error ($ pserrormsg)
{
$ Errors = & New helloerrors;
$ Errors-> adderror ($ pserrormsg );
}

?>

Next, we start the session and create several objects we will use.
Smarty template object
Hellomap Class Object (previously defined)
Phrame actioncontroller object
(It requires an array of options, which can be obtained by mappingmanager: getoptions)

This Code also adds an error model that encapsulates the actual implementation scheme of error handling.
(Save the error in $ _ session data. For more information, see helloerrors. php)

Two functions are defined.
Appl_error (), allowing you to add an error to the system error object.
Handle_error () is a PHP error handler function that calls the appl_error () function ()
This means that you can directly use the appl_error () function or
Trigger_error () (if the PHP error handling handle is set to handle_error ())

<?

If (array_key_exists (_ Action, $ _ Request ))
{
// Release control to Controller
// Further Processing
$ Controller-> process ($ map-> getmappings (),
$ _ Request );
}
Else
{
// Determine and display view
$ Requested_view = (array_key_exists (_ view,
$ _ Request ))?
Strtolower ($ _ Get [_ view]):
'Index ';
Switch ($ requested_view ){
Case 'hello ':
$ Template = $ requested_view. '. TPL ';
// Assign view specific data
$ Person = & new person;
$ Smarty-> assign ('name ',
$ Person-> getname ());
Break;
Case 'index ':
Default:
$ Template = 'index. TPL ';
}
// Assign Common Data
$ Smarty-> assign (Array (
'Appl _ link' => appl_actn
, 'Appl _ view' => appl_view
, 'Action' => _ Action
));
// Assign and clear errors
$ Smarty-> assign ('errors ',
$ Errors-> geterrors ());
$ Smarty-> display ($ template );
Exit;
}
The remaining code implements the Controller component of MVC.
The IF statement determines whether an action is requested.
If yes, call phrame
Actioncontroller: The process () method activates this framework.
If no request is specified, a view is displayed.

Else statement to determine the appropriate view,
Assign specific data to smarty Template
Assign common data to smarty Template
Handle any possible errors
Then render rendering Template

Jump out of this program and let's see what is included in our application to use phrame.
First, consider what our application should do if there is no parameter input at all.
In this example, skip action processing and render the index. TPL template to display the default view.
This is part of smarty template index. TPL.

<Form action = "{$ appl_link}" method = "Post">
<Div>
<Input type = "hidden" name = "{$ action}" value = "sayhello"/>
{If $ errors | @ count GT 0}
<Ul>
{Section name = e loop = $ errors}
<Li> <B style = "color:
Red ">{$ errors [e]} </B> </LI>
{/Section}
</Ul>
{/If}
What is your name? <Br/>
<Input type = "text" name = "name" value = "{$ name }"
/>
<Input type = "Submit" value = "OK"/>
</Div>
</Form>

It is important to check what is in progress.
This HTML form is submitted to the application script itself, which has a hidden variable,
Name the template variable $ action as "sayhello ",
This is the guidance for the Controller to initialize the "sayhello" Mapping input.

The following section of the template is error condition check. If an error occurs, an unordered list is displayed.
The last part of the template is the display part of the common form, a users Name text input box and a submit button.

So what happens when the user clicks the submit button?
The browser will submit an http post to this Bootstrap script.
The $ _ request [_ Action] value is "sayhello". As a response,
$ Controller will execute a perform () method
Review mapping, "sayhello", and helloaction in our hellomap class
The actioncontroller: perform () method will create an instance of the helloaction class and
Execute the helloaction: perform () method. See the following code.

Class helloaction extends action
{
Function & perform (& $ poactionmapping, & $ poactionform)
{
$ Person = & new person ();
$ Errors = & New helloerrors;
$ Name = $ poactionform-> get ('name ');
// Get actionforward depending on if
// Errors were generated
If ((! $ Person-> setname ($ name) | ($ errors-> haserrors ()))
{
$ Actionforward = $ poactionmapping-> get ('index ');
}
Else
{
$ Actionforward = $ poactionmapping-> get ('hello ');
}
Return $ actionforward;
}
}

Every action required in your application is similar to this file.
You can create a subclass of phrame action and reload the perform () method to implement an action.
Finally, your program needs to return an actionforward object to the Controller for processing.

Let's take a look at the details of the helloaction code.
At first, two specified objects are passed to the function perform in the address-passing mode for performance reasons.
This function will return the actionforward object as an address.
Then, create two instances of the model $ person and $ errors.
$ Poactionform is an instance of the phrame actionform class created by the Controller Based on your Mappings
We specify our form as actionform, instead of inheriting it, because there is no necessary reason
The actionform class inherits the hashmap class of the phrame tool class and is associated with data pre-loading with $ _ request.
Therefore, after the post is submitted, the $ poactionform-> get () method will get the posted variable.
In this example, we want to find the name entered in the text input box submitted by the user.
Then, check whether the person: setname () method in medel is successful,
Our action determines which actionforward mapping is returned.
At the same time, it also verifies that this actionr result has no error triggering. If there is a problem,
It will return to the "Index" view. Otherwise, the "hello" view is displayed.

Finally, let's take a look at the person. php file. For every application,
Models are unique, so the phrame library cannot have its prototype,
You do not have to inherit any classes from the phrame class. This model uses two constants.
And use them as the prefix of model class names to avoid duplicate names.

Looking back at the internal details of our model, we found that the name is stored in the PHP session.
Note that it is outside of our class, and the details of this storage are unknown, you can at any time
Change the class definition to change the implementation method.
In our application, all the operations on the session data index are completed through a single model class.
You can create correct error handling and business logic (similar to validation rules)
And enhanced the Data Consistency of your applications.
This is the contribution of MVC, facilitating the better maintenance of data in persistent storage.

Another interesting feature in this model is the setname () method,
In particular, our business logic requires that a name must be less than 20 characters long,
If you call this model with a name length value out of the range, an error is triggered.

Future Direction
How to expand the phrame-based MVC application?
If you need an additional data view, you can simply add a new template.
The "case" statement is extended to determine a valid view in the bootstrap file.

To add a new action, you must create a new class that inherits the action.
Just like the helloaction class we wrote, you also need
Add a mapping to let MVC know which class should be initialized when a new action is requested
Finally, when the action is complete, you need to add forwards to point to the appropriate view.

Most model classes we develop use databases,
Most of the time, we have two basic changes to phrame.
First, implement a default action to display a view
Secondly, the object-oriented style is not fully implemented in this example, and the factory mode can be used.
Create an instance of the application view in the default "showview" action.

Another problem in this example is that file inclusion is inefficient:
Each request must contain the action class, or even display the view of the result of a special request.
You can reset your application to be more sensitive to context and only include necessary resources.

Looking back, we have seen the benefits of using the MVC framework to implement PHP.
We also checked a model that uses session as persistent data storage,
How to Use hellomap, phrame, and actioncontroller to control the application process,
How to Use smarty templates in applications to implement View

This example is very simple, but it demonstrates how to process user input, data validation, error processing, and application processes.
By studying this example, we have the Foundation to develop MVC-based web programs.

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.