PHP MVC architecture implementation

xiaoxiao2021-03-06  42

Building the hello application

Step 1: Directory Structure for Hello Application

Following is The Recommended Directory. This Structure Can Be ModiFied So Long As the include.php file contains the correct paths to the files needed.

/ Hello

/ actions

/ Forms

/ incrude

/ models

/ views

Step 2: Building the model

The first step in building an application in Phrame is to set up the data models. If errors are generated in the models, Phrame can be notified by registering an error handler in the options.php and triggering an error with the trigger_error () php method FOLLOWING IS The Person Class for the Hello Application:

_ name = $ name;} function getname () {Return $ this -> _ name; Function setName {$ isvalid = true; if (Strlen> Max) {Trigger_ERROR ('name>' .max. 'Characters'); $ isvalid = false;} else {$ this -> _ name = $ name;} RETURN $ isvalid;}}?>

Also, these Models That Are Created Could Make Connections To Whichever Database You Are Using. All Data Manipulation Methods As Well As Database Transaction Methods Should Be Owned by Thase Models.

Step 3: Building the Actions

THE NEXT Step in building the application. These actions. The application........................................ ..

get ( 'name'); // get ActionForward depending on if errors were generated if ($ Person-> SetName ($ name)) || ($ _SESSION [_ERRORS])) {$ actionforward = $ actionmapping-> get ('index');} else {$ actionforward = $ actionmapping-> Get 'Hello'); // Put Person in The session $ _SESSION ['Person'] = $ Person;}}}}}}?> Step 4: Building the views

This step in building the application is setting up the views for your application. This should include all of the forms that you will need for your application. These can be written using XML / XSLT, PHP, Flash, etc. All views should have a Hidden Element Called Action So That The Controller Knows What Action To Take WHEN A User Does Something On a View Rendered In Xslt for the Hello Application:

FileName: index.xsl

View Description: This is the view that is initially displayed It's a basic form that allows you to enter a name If an error occurs when you submit, this view is shown again with the existing errors shown above the form...

phrame - Hello Application </ Title> </ HEAD></p> <p><body></p> <p><form action = "phrame.php" Method = "post"></p> <p><xsl: for-each select = "$ errors"></p> <p><XSL: Call-Template Name = "Error" /></p> <p></ xsl: for-energy></p> <p><p> What is your name? <br/></p> <p><Input Type = "text" name = "name" value = "{$ name}" /></p> <p><Input Type = "Submit" value = "ok" /> </ p></p> <p><Input Type = "Hidden" name = "action" value = "Sayello" /></p> <p></ form></p> <p></ body></p> <p></ html></p> <p></ xsl: template></p> <p><XSL: Template Name = "Error"></p> <p><li> <b style = "color: red"> <xsl: value-of select = "value" /> </ b> </ li></p> <p></ xsl: template></p> <p></ xsl: stylesheet></p> <p>FILENAME: Hello.xsl</p> <p>View Description: this is the result page there is shown if no errors ipcurred. It prints out "Hello {name}".</p> <p><? XML Version = "1.0"?></p> <p><XSL: Stylesheet Version = "1.0" XMLns: XSL = "http://www.w3.org/1999/xsl/transform"></p> <p><XSL: Output method = "html" /></p> <p><xsl: incrude href = "common.xsl" /></p> <p><XSL: Template Match = "Hello"></p> <p><html></p> <p><HEAD></p> <p><meta name = "generator" content = "html tidy, see www.w3.org" /></p> <p><title> phrame - Hello Application </ Title></p> <p></ hEAD></p> <p><body></p> <p><p> hello <xsl: value-of select = "$ name" /> </ p></p> <p><a href="index.php"> back </a> </ body></p> <p></ html></p> <p></ xsl: template></p> <p></ xsl: stylesheet></p> <p>FILENAME: Commons.xsl</p> <p>View Description: this is the commit used IN Both index.xsl and hello.xsl.</p> <p><? XML Version = "1.0"?></p> <p><XSL: Stylesheet Version = "1.0" XMLns: XSL = "http://www.w3.org/1999/xsl/transform"></p> <p><xsl: variable name = "errors" select = "Hello / ErrorS / Stack / _Elements" /></p> <p><xsl: variable name = "valid" select = "Hello / Models / Person / _name" /></p> <p><xsl: variable name = "invalid" select = "hello / forms / helloform / _values ​​/ value [@ key = 'name']" /></p> <p><xsl: variable name = "name" select = "$ valid [(not ($ errors))] | $ invalid [$ errors]" /></p> <p></ xsl: stylesheet></p> <p>Step 5: Building The Form Objects</p> <p>These objects extend the ActionForm object and are used to validate your application in PHP if for any reason you do not want to use other methods of validation (ex. JavaScript). If you do not need to validate using PHP, then do not worry about Creating One of these form Objects. FOLLOWING IS The Form Object in The Hello Application:</p> <p><? phpClass Helloform Extends Actionform {Function Validate () {$ Isvalid = true; if (! $ this-> get ('name')) {Trigger_ERROR ('Enter Your Name'); $ IsValid = false;} Return $ IsValid }}?></p> <p>Step 6: Piecing Together The Hello Application</p> <p>In Order to Piece Together Your Application, You Must Also Include The Following Information:</p> <p>Mapping Information Error Handling Information Application Options Bootstrap File View Handler All Application Files</p> <p>Mapping Information Should Be Located Inside The Mappings.php File (In The include Directory) As Follows:</p> <p><? Php // build mapping information to pass into controller $ mappings = array (_ACTION_FORMS => array ( 'form' => array (_TYPE => 'HelloForm')), _ACTION_MAPPINGS => array ( 'sayHello' => array ( _Type => 'helloaction', _name => 'form', _input => 'index.php? View = views / index.xsl', _validate => 1, _action_forwards => array ('Hello' => Array (_path = > 'index.php? view = views / hello.xsl', _redirect => 0), 'index' => Array (_path => 'index.php? view = views / index.xsl', _redirect => 0) )))))))); "Error Handling Information Should Be Located Inside The Errorhandler.php File (in The include Directory) As Follows:</p> <p><? PHPFunction HandleError ($ Number, $ Message, $ File, $ Line, $ Context) {if (! $ _ session [_ERRORS]) {$ Errors = new stack (); $ _SESSION [_ERRORS] = $ errors;} Errors = & $ _ session [_ERRORS]; $ Errors-> Push ($ message);}?></p> <p>Application Options Should Be Located Inside The Options.php File (In The include Directory) As Follows:</p> <p><? php // set options for the controller $ options = array (// set the _cache = 1 for data caching. _Cache => 1, // set to e_all to get controller errors (debug.com "_ERROR_REPORTING => E_USER_ERROR | E_USER_WARNING | E_USER_NOTICE, // Sets the error handler to the function that you specify _ERROR_HANDLER => "handleError",);> Bootstrap file, phrame.php, is where you GET / POST and releases control to the controller for further processing:?</p> <p>? <Phpinclude ( 'include / include.php'); session_start (); // add controller to session if not already cachedif {$ controller = new ActionController ($ options) ($ _ SESSION [_CONTROLLER]!); $ _SESSION [_CONTROLLER ] = $ controller;} // Release Control to Controller for further processing $ controller = & $ _ session [_controller]; $ controller-> process ($ mapings, $ _REQUEST);?></p> <p>View Handler, Index.php, Enables the usage of xlt stylesheets to process the xml representations of the response objects:</p> <p><? phpinclude ('include / include.php'); session_start (); // build "wrapper" xml to render comple $ xml = "<? XML Version =" 1.0 "?> / n" $ xml. = "<example> / n" $ xml. = "/ t <errors> / n" IF ($ _SESSION [_ERRORS]) {$ xml. = Xml :: Marshal ($ _ session [_ERRRORS], 2) } $ xml. = "/t</errors>/n" $ $m.=" / t <forms> / n "if ($ _SESSION [_FORM]) {$ xml. = Xml :: Marshal ($ _ session _Form], 2);} $ xml. = "/T</FormS>/n"$ xml.=" / t <models> / n "if ($ _SESSION ['Person']) {$ XML. = XML :: Marshal ($ _ session ['Person'], 2);} $ xml. = "/t</models>/n"$ € =" </ example> / n "$ _ session [_errors] = null; $ _SESSION [_FORM] = NULL; $ _ session ['Person'] = null; // Release Control TO View for firm rendering $ xsl = ($ _get [_view])? $ _Get [_view]: 'Views / Index.xsl '; $ result = Xml :: transform ($ xml, array ($ xsl), $ _GET); echo $ result; All Application Files that you have created should be included inside the include.php file (in the include directory) as Follows:</p> <p><? php // first include all the phrame base classes, Then Any Project SubclassesInClude ('../../ include.php'); include ('Forms / Helloform.php'); include ('actions / helloaction.php '); include (' MODELS / PERSON.PHP '); include (' Errorhandler.php '); include (' mappings.php '); include (' Options.php ');?>>>></p></div><div class="text-center mt-3 text-grey"> 转载请注明原文地址:https://www.9cbs.com/read-59849.html</div><div class="plugin d-flex justify-content-center mt-3"></div><hr><div class="row"><div class="col-lg-12 text-muted mt-2"><i class="icon-tags mr-2"></i><span class="badge border border-secondary mr-2"><h2 class="h6 mb-0 small"><a class="text-secondary" href="tag-2.html">9cbs</a></h2></span></div></div></div></div><div class="card card-postlist border-white shadow"><div class="card-body"><div class="card-title"><div class="d-flex justify-content-between"><div><b>New Post</b>(<span class="posts">0</span>) </div><div></div></div></div><ul class="postlist list-unstyled"> </ul></div></div><div class="d-none threadlist"><input type="checkbox" name="modtid" value="59849" checked /></div></div></div></div></div><footer class="text-muted small bg-dark py-4 mt-3" id="footer"><div class="container"><div class="row"><div class="col">CopyRight © 2020 All Rights Reserved </div><div class="col text-right">Processed: <b>0.044</b>, SQL: <b>9</b></div></div></div></footer><script src="./lang/en-us/lang.js?2.2.0"></script><script src="view/js/jquery.min.js?2.2.0"></script><script src="view/js/popper.min.js?2.2.0"></script><script src="view/js/bootstrap.min.js?2.2.0"></script><script src="view/js/xiuno.js?2.2.0"></script><script src="view/js/bootstrap-plugin.js?2.2.0"></script><script src="view/js/async.min.js?2.2.0"></script><script src="view/js/form.js?2.2.0"></script><script> var debug = DEBUG = 0; var url_rewrite_on = 1; var url_path = './'; var forumarr = {"1":"Tech"}; var fid = 1; var uid = 0; var gid = 0; xn.options.water_image_url = 'view/img/water-small.png'; </script><script src="view/js/wellcms.js?2.2.0"></script><a class="scroll-to-top rounded" href="javascript:void(0);"><i class="icon-angle-up"></i></a><a class="scroll-to-bottom rounded" href="javascript:void(0);" style="display: inline;"><i class="icon-angle-down"></i></a></body></html><script> var forum_url = 'list-1.html'; var safe_token = 'a3L7edsLt54KNzfDVQ5fx5_2B4PWYSezPekQ6zWyisB8xHeQ3NPJSU8PmPE0e9U8_2FlzPNcQXdcCajAedmxOqFIEQ_3D_3D'; var body = $('body'); body.on('submit', '#form', function() { var jthis = $(this); var jsubmit = jthis.find('#submit'); jthis.reset(); jsubmit.button('loading'); var postdata = jthis.serializeObject(); $.xpost(jthis.attr('action'), postdata, function(code, message) { if(code == 0) { location.reload(); } else { $.alert(message); jsubmit.button('reset'); } }); return false; }); function resize_image() { var jmessagelist = $('div.message'); var first_width = jmessagelist.width(); jmessagelist.each(function() { var jdiv = $(this); var maxwidth = jdiv.attr('isfirst') ? first_width : jdiv.width(); var jmessage_width = Math.min(jdiv.width(), maxwidth); jdiv.find('img, embed, iframe, video').each(function() { var jimg = $(this); var img_width = this.org_width; var img_height = this.org_height; if(!img_width) { var img_width = jimg.attr('width'); var img_height = jimg.attr('height'); this.org_width = img_width; this.org_height = img_height; } if(img_width > jmessage_width) { if(this.tagName == 'IMG') { jimg.width(jmessage_width); jimg.css('height', 'auto'); jimg.css('cursor', 'pointer'); jimg.on('click', function() { }); } else { jimg.width(jmessage_width); var height = (img_height / img_width) * jimg.width(); jimg.height(height); } } }); }); } function resize_table() { $('div.message').each(function() { var jdiv = $(this); jdiv.find('table').addClass('table').wrap('<div class="table-responsive"></div>'); }); } $(function() { resize_image(); resize_table(); $(window).on('resize', resize_image); }); var jmessage = $('#message'); jmessage.on('focus', function() {if(jmessage.t) { clearTimeout(jmessage.t); jmessage.t = null; } jmessage.css('height', '6rem'); }); jmessage.on('blur', function() {jmessage.t = setTimeout(function() { jmessage.css('height', '2.5rem');}, 1000); }); $('#nav li[data-active="fid-1"]').addClass('active'); </script>