Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Over the Wall MVC

Over the Wall MVC

MagicPHP is a small PHP library that allows developers to create a rich user experience and reduce code redundancy.

Dee Wilcox

March 10, 2015

Transcript

26 slides
  1. What is the Wall? • In any given web application,

    there is a wall between client side code and server side code. • In the past, we were stuck with slow form postbacks and a poor user experience. • Today, with jQuery and Ajax on our side, we can create solid, secure web applications that are also user friendly. • This allows us to go “over the wall.”
  2. The Problem custom.js $("#ajaxform").submit(function(e){ var postData = $(this).serializeArray(); var formURL

    = $(this).attr("action"); $.ajax({ url : formURL, type: "POST", data : postData, success:function(data, textStatus, jqXHR){ //data: return data from server }, error: function(errorThrown){ } }); e.preventDefault(); e.unbind(); }); $("#ajaxform").submit(); • We write more AJAX than we do PHP (or at least more than we would like). • Software and data driven applications require custom AJAX functions for every button click and form submit.
  3. Over the Wall MVC • Over the wall MVC is

    an integrated approach to utilizing AJAX within our PHP code. • A standard set of functions handles most view requests and is called within a PHP function. • This helps us reduce code redundancy and keep our code DRY. • It also frees us up to do what we do best.
  4. Why Create MagicPHP? • We have used XAJAX (http://www.xajax-project.org) at

    NASBA for the last few years. However, it is unsupported and seriously out of date. • We wanted to use the latest features of jQuery without creating conflicts. • We wanted a lighter-weight, cleaner library.
  5. How MagicPHP Works • User action like an onclick event

    fires a request to $magicRequestManger(). • $magicRequestManager() validates and invokes the $magicRequest() object. • $magicRequest() calls the appropriate $magicResponse() method. • $magicResponse() sends “preformatted” javascript back to the browser utilizing methods in the magic.js plugin.
  6. Exploring magic.php • The core class file is Magic (magic.php).

    • It invokes the magicRequest, magicResponse, magicFunctions, and magicSettings objects. • Primarily, it checks for valid session handling to determine whether requests can be processed. • It also contains the getJavascript() method, which is returned in the template file. • Other features like buffering and logging can also be added.
  7. Exploring magic.php public function __construct(){ $this->magicCanProcessRequest = false; $this->magicErrorHandler =

    false; $this->magicLogFile = false; $this->magicRequestObject = new MagicRequestManager(); $this->magicResponseObject = new MagicResponse(); $this->magicFunctionsObject = new MagicFunctions(); $this->magicSettings = new MagicSettings(); $this->magicErrorHandler = $this->magicSettings->errorHandler; $this->magicCleanBuffer = $this->magicSettings->errorHandler; $this->magicLogFile = $this->magicSettings->errorHandler; $this->magicExitAllowed = $this->magicSettings->exitAllowed; }
  8. Exploring magic.php public function receiveRequest($requestType, $requestFunction) { if($this->magicCanProcessRequest) { if($requestType

    == 'magicResponse') { if(method_exists($this->magicResponseObject,$requestFunction)) { $this->magicResponseObject->$requestFunction; } else{ $this->magicResponseObject->alert('The requested function does not exist.'); } } else if ($requestType == 'magicFunction') { if(method_exists($this->magicFunctionsObject,$requestFunction)) { $this->magicFunctionsObject->$requestFunction; } else{ $this->magicResponseObject->alert('The requested function does not exist.'); } } ….
  9. Exploring magic.php public function getFormValues($formId) { if($formId) { $javascript =

    <<<JS $.fn.magicGetFormValues(‘#$formId’); JS; return $javascript; } else { return false; } }
  10. Exploring magic.js • Magic.js is a simple jQuery plugin containing

    a few key methods. ◦ magicAlert ◦ magicAssign ◦ magicCallFunction ◦ magicGetFormValues • It can be expanded to add more functionality for your application.
  11. Exploring magic.js $.fn.magicAssign = function( elementId, data ) { var

    element = jQuery('#' + elementId); if( typeof elementId == undefined) { debug('assign','Element is undefined'); } else if (elementId == null) { debug('assign','Element is null'); } else { jQuery('#' + elementId).html(data); } };
  12. Exploring magic.js $.fn.magicCallFunction = function ( functionName, formValues ){ $.ajax({

    url: "/", data: { requestFunction:functionName, requestParameters: formValues, }, type: "POST", dataType: "json", success: function(data) { }, error: function (data) { }, complete: function(data){ } }); };
  13. The Demo Site Beer vs Coffee Which is better for

    your brain? http://ec2-52-1-170-248. compute-1.amazonaws. com/
  14. The Demo Site Click the button, get an email with

    some fun information about how creative work can be enhanced by beer and coffee.
  15. Setting Up magicPHP • Make sure your application is running

    the latest version of jQuery. • Install the magicphp folder in the vendor directory and include in the core PHP file. • Install magic.js inside the public directory. include(PRIVATE_BASE . 'vendor/magicphp/magic.php'); use Magic\Magic; $magicObject = new Magic(); $magicResponse = $magicObject->magicResponseObject(); $magicJS = $magicResponse->getJavascript(); $magicResponse->processRequest();
  16. Usage Example: Forms <div id="signup"> <span class="icon-close toggle" id="close"></span> <form

    id="coffee" name="coffee" action="/" method=" POST"> <div id="signup-inner"> <input class="name text" id="first_name" name=" first_name" type="text" placeholder="First Name"/> <input class="name text" id="last_name" name=" last_name" type="text" placeholder="Last Name"/> <input class="email text" id="email" name="email" type="text" placeholder="Email Address"> </div> <input type="submit" class="submit" id="submit" name="submit" value="Give me an IPA, please"> </form> </div> We have a traditional HTML form with a unique ID and a few form inputs. Typically, we trigger an onclick event with the submit button.
  17. Usage Example: Forms $("#beerSubmit").click(function(){ var htmlText = '<div id="beerSignupInner"><h1>You guessed

    it!</h1><h2>Beer is better than beer... for creative brainstorming.</h2></div>'; $.ajax({ url: "/contact.php", data: { requestFunction:'contact_save', requestParameters: 'none', }, type: "POST", dataType: "json", success: function(data) { }, error: function (data) { }, complete: function(data){ jQuery('#beerSignup').html(htmlText); }}); }); $subject = "You're Right! Beer is Better than Coffee... for Creativity"; $first = $_REQUEST['beerFirstName'] ; $last = $_REQUEST['beerLastName'] ; $email = $_REQUEST['beerEmail'] ; /* Define $headers and $message */ $headers = '’; $message = ‘’; $sent = mail($email, $subject, $message, $headers); if($sent) { header('Location: thank_you.html'); } else { header('Location: error.html'); }
  18. Usage Example: Forms <div id="signup"> <span class="icon-close toggle" id="close"></span> <form

    id="coffee" name="coffee" action="/" method=" POST"> <div id="signup-inner"> <input class="name text" id="first_name" name=" first_name" type="text" placeholder="First Name"/> <input class="name text" id="last_name" name=" last_name" type="text" placeholder="Last Name"/> <input class="email text" id="email" name="email" type="text" placeholder="Email Address"> </div> <input type="button" class="submit" id="submit" name="submit" value="Americano, please" onclick="$.fn. magicCallFunction('contact_save',$.fn.magicGetFormValues ('coffee'));" > </form> </div> With magicPHP, we can make one call magicCallFunction in the onclick event itself.
  19. Usage Example: Forms Then, we process the data and handle

    population of the view from our private PHP function. function contact_save($formValues) { if(!empty($formValues)){ $email = $formValues['coffeeEmail'] ; /* Define $subject, $message, $headers ... */ $sent = mail($email, $subject, $message, $headers); if($sent) { $objResponse->assign('coffeeSignup',$dialogHTML); } else { $objResponse->alert('Problem!'); } } else{ $objResponse->alert('Problem!'); } }
  20. See the Difference • In the traditional example, we have

    to wait for the form to submit and the email to send. The magicPHP is dramatically faster. • magicPHP also gives us cleaner code. We are re-using the same method in magic.js, as compared to writing at least 20 lines of AJAX just for that single form submit.
  21. Links • magicPHP on Github: https://github. com/deewilcox/magicphp • magicPHP Demo

    site on Github: https: //github.com/deewilcox/magicphp_demo • Demo site: http://ec2-52-1-170-248.compute- 1.amazonaws.com/
  22. Stay in Touch • Twitter: @dee_wilcox • Github: https://github.com/deewilcox •

    Slides: https://speakerdeck. com/deewilcox/over-the-wall-mvc

More Decks by Dee Wilcox

Other Decks in Programming