Simple guest book - mhmxs/lisaframework GitHub Wiki

Simple guestbook example

Register guestbook urls

The guestbook application has three function, list the entries, add new entry, and remove unusable entry, so you need to register 3 urls in config/urls.cfg file,

'/guestbook' => array('controller' => 'GuestBook','function' => 'entries'),
'/guestbook/add' => array('controller' => 'GuestBook','function' => 'addEntry'),
'/guestbook/delete/([0-9]*)' => array('controller' => 'GuestBook','function' => 'deleteEntry', 'urlParams' => array("id")),

Create database

Create a database gb_example, and create the user for this database, and set this parameters in config/MySQL.ini file. After this create questbook table.

CREATE DATABASE gb_example;
CREATE TABLE `gb_example`.`guestbook` (
`id` INT( 11 ) NOT NULL AUTO_INCREMENT PRIMARY KEY ,
`comment` TEXT NOT NULL ,
`author` VARCHAR( 255 ) NOT NULL
) ENGINE = MYISAM;

MySQL.ini

[default]
db_host = localhost
db_name = gb_example
db_user = user
db_password = password
db_charset = utf8

Create controller

You need to create controller in libraries/Controller directory. The file name of the controller must be GuestBookController.php, and the content is this code:

namespace Controller;
class GuestBook extends \Core\Controller\Controller
{
    public function entries()
    {
	$dao = \Core\DAO\Builder::start("guestbook")->build();
        $this->_view->setVar("entries", $dao->getAll());
        $this->_view->setTemplate("guestbook.html");
    }

    public function addEntry()
    {
        $dao = \Core\DAO\Builder::start("guestbook")->build();
        $entity = $dao->getNew(array("comment" => $this->_post->comment, "author" => $this->_post->author));
	$dao->commit($entity);
        \Core\HTTP\HTTP::redirect("/guestbook");
    }

    public function deleteEntry()
    {
        $dao = \Core\DAO\Builder::start("guestbook")->build();
        $dao->delete($dao->getOne($this->_get->id));
        \Core\HTTP\HTTP::redirect("/guestbook");
    }
}

If you want to use entities as objects you can do it, like this :

    public function addEntry()
    {
        $dao = \Core\DAO\Builder::start("guestbook")->build();
        $entity = $dao->getNew();
        $entity->comment = $this->_post->comment;
        $entity->author = $this->_post->author;
        $dao->commit($entity);
        \Core\HTTP\HTTP::redirect("/guestbook");
    }

for more information see see ModelInLISA Model in LISA.

Create template

You need to create your template in webroot/templates directory, and name it to guestbook.html, and put this lines in it:

<h1>Guestbook</h1>
<h2>Entries</h2>

{foreach from=$entries item=i}
   <p>{$i->comment}</p>
   <p>{$i->author} (<a href="/guestbook/delete/{$i->id}">delete</a>)</p>
   <hr />
{/foreach}

<h2>New entry</h2>
<form action="/guestbook/add" method="post">
   <textarea name="comment"></textarea><br />
   <input type="text" name="author" /><br />
   <input type="submit" value="Send" />
</form>

How to find mistakes and bugs

Lisa defaultly hide all errors from frontend, but store them in log files. You can find the log files in tmp/logs directory. The logging system separate errors by level, and place them into process.log, debug.log, error.log, database_error.log files.

⚠️ **GitHub.com Fallback** ⚠️