Tutorial
========
Setup 1 Projects
----------------
This section teaches you to build your project
in just a few steps with Ethna using **command line**.
.. hint::
Make sure once the Ethna is installed on your computer, you have made
changes to your ``php.ini`` files. i.e. pear libraries are in the include
path. On a Mac, you would do something like the following
.. code-block:: bash
$ emacs /etc/php.ini
And then add the appropriate path
; include_path = ".:/Users/YOUR-USER-NAME/pear/share/pear/"
Similarly, make the changes to your php.ini file on other Operating Systems.
Simple Blog Application
+++++++++++++++++++++++
Here, we are going to create a simple blog application with Ethna and ethna's command line tool.
In this tutorial, there are two important learnings
1. ``Ethna`` flow of creating an application
2. How to set up a url
1. Application information
+++++++++++++++++++++++++++
Before developing an Application, there are two points to keep in mind.
1. Application ID (*must be letters*)
e.g. miniblog
2. Application Directory. Usually on Linux, it is ``/var/www/``. Although, you may change it your directory by modifying the
``/etc/apache/httpd.config`` file.
In this tutorial, we are going to use ``/var/www/``.
To create a new project command ``ethna add-project AwesomeName``
.. code-block:: bash
$ cd /var/www
$ ethna add-project miniblog
The above command will create a skeleton for the application. Following is the sample output.
.. code-block:: bash
$ ethna add-project miniblog
creating directory (/var/www/miniblog) [y/n]: y
project sub directory created [/var/www/miniblog/app]
project sub directory created [/var/www/miniblog/app/action]
...
file generated [/usr/share/php/Ethna/skel/skel.view_test.php -> /var/www/miniblog/skel/skel.view_test.php]
project skeleton for [miniblog] is successfully generated at [/var/www/miniblog]
Above application has been generated.
Verifying the Directory Structure
+++++++++++++++++++++++++++++++++
Let's take a peek at the directory and the files generated.
::
| - App (directory of the application)
| | - Action (Action Script)
| | - plugin (filter script)
| | - test (test scripts)
| `- View (view script)
| - bin ( command line scripts)
| - etc (configuration files, etc.)
| - lib library) (application
| - locale
| `- ja_JP
| - log (log file)
| - schema (DB schema, etc.)
| - - skel (skeleton application file)
| - template
| `- ja_JP (template file)
| - tmp (temp files)
`- www (web publishing file)
Common use is in the following directory.
- ``app/``
- ``app/action``
- ``app/view``
- ``template/ja_JP``
2. Setting Application's Url
++++++++++++++++++++++++++++
Although you may see it on the ``localhost/miniblog/www``. Also, in order to access the application from web browser, i.e. ``var/www/``.
We are going to set the **Apache Virtual Host** to ``http://miniblog.myhost/`` like the following:
::
{$app.helloVariable}
Implementation of Application ------------------------------ Lets start the implementation of creating a blog via learning following 3 things 1. Add action to handle the post, defined in a form. 2. Output the value from the form. 3. Validate the values from input form. Add action ++++++++++ First is to quickly add an action named ``commit`` by the command ``ethna add-action commit`` .. code-block:: bash $ ethna add-action commit file generated [/var/www/miniblog/skel/skel.action.php -> /var/www/miniblog/app/action/Commit.php] action script(s) successfully created [/var/www/miniblog/app/action/Commit.php] As the message also shows, there should be a file generated in ``app/action/Commit.php`` Here, there are two classes, 1. ``class Miniblog_Form_Commit extends Miniblog_ActionForm`` that handles the input fields for the form 2. ``class Miniblog_Action_Commit extends Miniblog_ActionClass`` that performs actions upon input/submitting the form Lets take a look at them one by one Creating Form +++++++++++++ And in ``app/action/Commit.php`` file. .. code-block:: php array( 'type' => VAR_TYPE_STRING, 'form_type' => FORM_TYPE_TEXTAREA, 'name' => 'comment', 'max' => 140, 'required ' => true, ), ); } Which is: - Form named **Comments** - The value type is string - Form type is TEXTAREA - 140 Characters - Required Item And the second class .. code-block:: php Post {form name="form_comment" ethna_action="commit"} Post Content: {form_input name="comment"} {form_submit} {/form} ``ethna_action`` defines the action to be called. It is similar to doing ``action="commit.php"`` in plain html form. Similarity other values are self explanatory. Upon visiting the browser, a page similar to following should appear. .. image:: ../images/tutorial_03-01.png :scale: 100 % :alt: alternate text :align: center Showing the POST content ++++++++++++++++++++++++ .. code-block:: html"; $i = 0; while ($data[$i] = $res->fetchRow()) { if ($i){ var_dump($data); } $i++; } echo ""; **INSERT** .. code-block:: php $sql = "INSERT INTO users (username, email, password, osid, gameid,signup) VALUES (?,?,?,?,?,now())"; $params = array( "kevin", "dummy@gmail.com", sha1("foo"), "1111", "gamers" ); $stmt = & $db->db->prepare($sql); $res = & $db->db->execute($stmt,$params); //Note that this message only exists in case of DB error if (method_exists($res, 'getMessage')){ var_dump($res->getMessage()); } **UPDATE** .. code-block:: php $sql = "UPDATE puzzle SET playedby=?,solved=?,score=score +100 WHERE id=?"; $params = array("johny", '1', "111"); $stmt = & $db->db->prepare($sql); $stmt = & $db->db->execute($stmt,$params); .. tip:: Check the other class methods quickly by ``var_dump(get_class_methods($res))``