Start your first application

Simple application
  1. Write relative action class :
    Action class is a place which is stored logic to control and can process all things which you want. (i.e. Store data into database, Get data from database, etc…) All action class must extend to "ActionBase" class. You can include this class by using "include_once ("ActionBase.php");". "execute" method is abstract method from "ActionBase" class. This method is main method. By default, "execute" method is called when this action class is called. Every action class must define this method. This method must return the string value. It is mapping value of the location of template file in XML action mapping file. Assume that the root folder of action class is "class/action/", we save this action class in "<project root>/class/action/TestAction.php".

    TestAction.php:
    <?php
    /* "ActionBase" class must be included and extended by all action class */
    include_once ("ActionBase.php");

    class Test Action extends ActionBase {

    /* Main method */
       public function execute() {
          return " success ";
       }
    }
    ?>

  2. Create HTML template file(s) for view part :
    Assume that the template root folder is "<project root>/template". We save this template page into "<project root>/template/result.html".

    result.html:
    <html>
    <head>
    <title>Simple application</title>
    </head>
    <body>successfully page</body>
    </html>

  3. Modify "default.actions.xml" to describe the relationship between action class and template file(s) :
    In every action class, we should define all "return" string value(s) and which template file(s) is/are represented to these "return" string value(s). In action tag, "actionPath" attribute defines the location of action class file in root folder of action class. "className" attribute defines the action class name. "name" attribute defines the keyword which is used to call action class. "view" tag defines what "return" string value is represented to template file. "name" attribute of "view" tag defines the value of "return" string and the content of "view" tag defines related template file. If you want to know more detail about action mapping file, you can refer "Tags in action mapping file" part.

    default.action.xml:
    <?xml version="1.0" encoding="UTF-8"?>
    <actions>
    <action actionPath="TestAction.php" name="Test" className="TestAction">
    <view name="success">result.html</view>
    </action>
    </actions>

  4. Afterwards, we can call this action class by entering following URL in browser :
    http://<Hosting address>/<project root>/ActionHandler.php?__action__=Test

  5. It returns "result.html" template page in browser :

HTML Form application
In this example, we want to provide simple HTML form for user to input their name and address. After submitting form, browser will show all input values in another page.

  1. Modify action class :
    Now, we modify the action class which is demo in previous example.
    • In this example, we take two values (name, address) from HTML form. Add two private parameters to store values of input boxes in HTML form.


    • We add "get" and "set" methods for each parameter. The rule of naming get / set method is same as naming of Java Bean get /set method. KEVIX will find out all set method(s) of parameter(s) in action class and set related value from HTML form to related private parameter(s) by using these set method(s). After processing in action class, KEVIX will find out all get method(s) of parameter(s) in action class and pass all value(s) of parameter(s) to template engine by using these get method(s).


    • We also add one of method which is called "submit" (i.e. the naming of method is up to you). It is because we want to call this method after submitting HTML Form. We can call this method by using following URL:
      http://<Hosting address>/<project root>/ActionHandler.php?__action__=Test !submit

      We can put this URL to "action" value of "form" tag in HTML form.

    TestAction.php :
    <?php
    include_once ("ActionBase.php");

    class Test Action extends ActionBase {
       private $address;
       private $name;

       public function getAddress(){
          return $this->address;
       }

       public function setAddress($address){
          $this->address = $address;
       }

       public function getName(){
          return $this->name;
       }

       public function setName($name){
          $this->name = $name;
       }

    /* Main method */
       public function execute() {
          return "form";
       }

       public function submit () {
          return "success";
       }
    ?>

  2. Modify and add HTML template file(s) for view part :
    Assume that the template root folder is "<project root>/template" and we choose "Smarty" for template engine. In this example, we need to create two template file. One of them is HTML form (form.html). Another one is used to show result (formResult.html).We save these template files into "<project root>/template" folder.

    form.html:

    <html>
       <head>
          <title>HTML Form application</title>
       </head>
       <body>
          <form name="form" method="post" action="ActionHandler.php?__action__=Test!submit">
          <table border="0">
          <tr>
             <td>Name : </td>
             <td><input name="name" type="text" id="name" size="50"></td>
          </tr>
          <tr>
             <td>address : </td>
             <td><textarea name="address" cols="50" rows="5" id="address"></textarea></td>
          </tr>
          <tr>
             <td>&nbsp;</td>
             <td><input type="submit" name="Submit" value="Submit">
                <input type="reset" name="Submit2" value="Reset"></td>
          </tr>
          </table>
          </form>
       </body>
    </html>

    formResult.html:
    name = {$name}<br>
    addess = {$address}

  3. Modify "default.actions.xml" to describe the relationship between action class and template file(s) :
    Now, we add and modify view tag values.

    default.action.xml :
    <?xml version="1.0" encoding="UTF-8"?>
    <actions>
       <action actionPath="TestAction.php" name="Test" className="TestAction">
             <view name="success">test.html</view>
             <view name="form">form.html</view>
       </action>
    </actions>
HTML Form application with validation
In this example, we modify previous example so that two input fields must be filled in data. Otherwise, it will show error messages in HTML Form and keep data which is entered before. There are two ways to perform validation .One of them is by using "Validation filter" (Please refer to "Filtering" part). Another one is by checking logic in action class. Now, we demo validation function by checking logic in action class.
  1. Modify action class :
    Now, we modify the action class which is demo in previous example.
    • We add "check" method to check whether "name" ,"address" fields' values is empty string or not.


    • In "check" method, "addError" method defines in "ActionBase" class. It is used to store error message and related error message keyword.
      addError( "<error message keyword>" , "<Error message>" )

    • In "submit" method, "hasError" method defines in "ActionBase" class. It is used to check whether program calls "addError" method to add error or not.

    TestAction.php :

    <?php
    include_once ("ActionBase.php");

    class Test Action extends ActionBase {
       private $address;
       private $name;

       public function getAddress(){
          return $this->address;
       }

       public function setAddress($address){
          $this->address = $address;
       }

       public function getName(){
          return $this->name;
       }

       public function setName($name){
          $this->name = $name;
       }

       public function execute() {
          return "form";
       }

       public function submit() {
          $this->check();

          if($this->hasError())
             return "form";
          else
             return "success";
       }

       public function check(){
          if( trim( $this->name ) =="")
             $this->addError("name","Please enter your name!");

          if( trim( $this->address ) =="")
             $this->addError("address","Please enter your address!");
       }
    }
    ?>


  2. Modify HTML template file(s) for view part :
    Now, we modify "form.html" file in previous example.
    • KEVIX will set all error messages and its related error message key into PHP Array which is called "errors". (i.e. $errors["<error message key>"] = "<error message>" )


    • We can give the error message(s) by using this Array variable.

    form.html :
    <html>
       <head>
          <title>HTML Form application with validation</title>
       </head>
       <body>
          <form name="form" method="post" action="ActionHandler.php?__action__=Test!submit">
             <table border="0">
                <tr>
                   <td>Name : </td>
                   <td><input name="name" type="text" id="name" size="50" value="{$name}"></td>
                </tr>
                <tr>
                   <td></td>
                   <td>{$errors.name}</td>
                </tr>
                <tr>
                   <td>address : </td>
                   <td><textarea name="address" cols="50" rows="5" id="address">{$address}</textarea></td>
                </tr>
                <tr>
                   <td></td>
                   <td>{$errors.address}</td>
                </tr>
                <tr>
                   <td>&nbsp;</td>
                   <td>
                      <input type="submit" name="Submit" value="Submit">
                      <input type="reset" name="reset" value="Reset">
                   </td>
                </tr>
             </table>
          </form>
       </body>
    </html>

  3. Result:
    • Without entering any data and press submit button


    • Entering data in one of text field