MVCnPHPCommand

From GeeklogWiki
Jump to: navigation, search

Your First Command

Setting up our View

Before we can really do anything useful with the command we will create we will first modify our view's Flexy template to have a form that will submit data to our command.

<html>
    <head>
        <title>{pageTitle}</title>
    </head>
    <body>
    <p>
    This is a page created using the Geeklog 2 Framework.  You are viewing this page on {getDateTime()}.
    </p>
    <p>
    {if:sum}
    The sum of your two numbers is {sum}.  Use the form below to add up two more numbers.
    {end:}
    <form method="POST" action="index.php?cmd=addNumbers">
    <b>First Number:</b><input type="text" name="firstNumber" /></br>
    <b>Second Number:</b><input type="text" name="secondNumber" /></br>
    <input type="submit" value="Add 'em Up" />
    </form>
    </p>
    </body>
</html>

The most notable thing is that in our form's action we have index.php?cmd=addNumbers. If you recall, that cmd GET string value tells our controller what it needs to do when we post the data. Note that if you prefer, you could have easily have added a hidden form field called cmd with a value of addNumbers and made the form action post to just index.php. That's because the MVCnPHP Controller listens for the cmd value on both the GET and POST superglobals. Also notice that we have introduced the use of a IF statement in our Flexy template. The template now checks to see if the view has a sum of two numbers, if so it will display the sum. Now that we have a form, let's build the command!

Building the Command

MVCnPHP Commands work very much the same way as views minus the need for Flexy. Let us start by creating a file called SAMPLE_addNumbersCommand.php:

/**
 * Abstract base command
 */
require_once 'Geeklog/MVCnPHP/BaseCommand.php';

class SAMPLE_addNumbersCommand extends MVCnPHP_BaseCommand {
    /**
     * Adds up the numbers we get
     * 
     * @access public 
     * @return string Name of forward (if any)
     *
     */
    public function execute()
    {    	        
        if (!is_numeric($_POST['firstNumber']) OR !is_numerice($_POST['secondNumber']) {
            return 'validationFailure';
        }

        $_REQUEST['sumOfNumbers'] = $_POST['firstNumber'] + $_POST['secondNumber'];

        return 'success';
    }
}


If you recall, the command pattern represents an action and the above is a very simple action that first checks to ensure we got two numbers to add up and, if so, it adds them up. You'll see that the execute() method returns a string which can be either validationFailure or success. Each of those strings is the name of a forward.

Forwards tell the controller where to send processing next. Both MVCnPHP views and commands can have forwards. How does the controller use these forward to figure out what to do next? Yep, you got it, from it's configuration file.

Updating the Controller's Configuration File

Before any of this will work, we need to let our controller know about the new command and configure the command's forwards:

<?xml version="1.0" encoding="UTF-8"?>
<mvc_configuration compile="true">	
    <view id="home" name="SAMPLE_HomeView" default="true" />    
    <command id="addNumbers" name="SAMPLE_addNumbersCommand">
        <forward id="success" type="view">home</forward>
        <forward id="validationFailure" type="view">home</forward>        
    </command>
</mvc_configuration>