(Walter.fan compiled from Jason E. SWEAT Write PHP) Next to see a simple MVC application instance: said that the sky is a lotus, it is better to act and make a simple example. In this example, we Implement the MVC mode with a Phrame frame. Phrame is a PHP implementation of Jakarta Struts, which is an XML file as Struts, but the most common array in PHP.
In Phrame, like Struts, the models and views are loosely coupled together through the Action, Forms and Forwards. Every Controller initializes, it creates an instance of the subclass of the Action class, CONTROLLER will call you. Action subclass's process () method, a list of Forwards and a FORM object containing all HTTP Request variables
Your Action's expectations are to determine that the appropriate Forward object returns to controller.controller handles this Forward object and exits the processing process for this request.
The action of an action object, can do a class between HTTP Request and a sentence than you can think that in this sentence, action is a verb, Model is a noun, and the view is an adjective. In other words, a specific The web request will execute an action on a model (noun), or display a view to describe a Model (noun)
This type describes the Action and Model relationships, and can consider when selecting an object name.
This example is a modified version of Phrame's Hello World example (http://phrame.itsd.ttu.edu/). Source code can be downloaded from http://sourceforge.net/projects/phrame in this example, I have dropped Some warnings of PHP, use Smarty instead of XSLT to format the view
The MVC application uses a bootstap file, which is a separate PHP file. This 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 'MappingManager.php'; require_once 'actions / HelloAction.php '; require_once' mode / person.php '; Require_once' modls / helloerrors.php '; define (' Appl_View ',' Hello.php? '._ view.' = '); Define (' Appl_Actn ',' Hello.php ');?> In a phrame program, the relationship between Forms and Forwards is built in the traditional phrame option array. I think this array monotonous length is not conducive to development and maintenance creation a mappingmanager class. Management Maintenance These relationships This class can be extended by any Phrame application, which has a way to return PhP arrays used by Phrame. The advantage of this class is to enhance their access to the relationship between Form and Action Mapping (avoiding You try to generate an error when using 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 ('SayHello', 'HelloAction', Appl_View.'index ',' Helloform '); $ this -> _ Addforward (' Sayello ' , 'index'); $ this -> _ addforward ('SayHello', 'Hello');}} During the Hellomap Class constructor, we have to do it. First, overload the traditional phrame An array of options, we define an error handling function Next, see what form needs to identify, in this example, there is no need to extend the standard Phrame Actionform class Finally, define Actions and Forwards, this example, only one action: SayHello two Forwards : "INDEX" and "Hello" mappingmanager :: _ addmapping () method are parameters (mapping name, implementing this mapping class, calling Action default address, mapping with action with action)
The parameters of the mappingmanager :: _ addforward () method are the identifiers of the action maping, the IRL of the Forward identifier, and an optional redirect URL (if there is no Action associated with it)
session_start (); $ market = & new smarty; $ map = & new hellomap; $ monitorler = & new actionController ($ map-> getOptions ()); $ error = & new helloerrors; function handle_ERROR ($ Number, $ Number, $ Number, $ Number, $ Number, $ Number, Message, $ FILE, $ LINE, $ Context) {appl_error ($ message);} Function Appl_ERROR ($ PSERRMSG) {$ Errors = & new helloerror; $ error-> adderror ($ pSERRORMSG);}?>
Next, we start the session, create a few objects we want to use Smarty Template ObjectHellomap Class Object (which is previously defined) Phrame ActionController Object (it requires an option array, can be obtained by mappingmanager :: getOptions ()
The code in this section also joined an error Model, encapsulated an error handling actual implementation (saving errors in one data of $ _SESSION, see HelloerRors.php)
And define two functions appl_error (), allowing you to add an error to the system's error object to the Handle_Error () a PHP error handler, which calls the function appl_error () that you can directly use the function Appl_ERROR (). Or use Trigger_ERROR () (if the PHP error handler handle is set to Handle_ERROR ())
if (array_key_exists (_ACTION, $ _ REQUEST)) {// release control to controller for // 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-> getRrors ()); $ smarty-> display; exit;} The remaining code implements the MVC's Controller component if statement determines whether to request an action. If yes, call phrameActionController :: Process Method activates this framework. If there is no specified request, display a ViewElse statement to determine the appropriate view, Assign specific data to the Smarty template Assign General data to the smarty template to process any possible error and then render Template
Jump out of this program, let's see what we are adding in our application to use phrame first, think about what if there is no parameters to pass in, our application should do. In this example, skip Action Processing, render Index .tpl template to display the default view here is part of Smarty Template Index.tpl