Posts Tagged ‘easy’

Using DooTranslator for translation

DooTranslator finnaly came out, it supports “Csv“, “Gettext“, “Array“, “Ini” for now, soon it will be implemented other “file types”. So for example you have your translation file with this contents:

en.svn

test;"This is just a test!"

Lets initialize DooTranslator class:

$translator = Doo::translator('Csv', $this->_basePath . 'languages/en/LC_MESSAGES/en.csv');
 echo $translator->translate("test");

As you can see its very simple, first argument is file type for translation and second is path to the file, third argument can be options array. For Csv you can set options like: delimiter, enclosure and length. Default value for delimiter is “;” and for enclosure is “.

Now we will add some options to our call, lets say we want some other delimiter then “;” and we want to add some caching.

$translator = Doo::translator('Csv', $this->_basePath . 'languages/en/LC_MESSAGES/main.csv', array('cache' => 'apc', 'delimiter' => '|'));

So now we have added cache mechanism that is “apc” and we added delimiter that is “|”, now in our en.csv we must change delimiter:

test|"This is just a test!"

Supported cache mechanisms are “apc”, “php”, “xcache” and “eaccelerator”.

Now lets add placeholders for translate method:

$translator->translate("Hello [_1], wellcome to my website!", array("John"));

This will show translated string with: “Hello John, wellcome to my website!”.

Ok thats about it, if you have any questions please do ask, this is simple translation class its still

More »

Using DooModel for Database Operations

If you have ever read DooPHP guide on model, you will see that a basic Model class does not have to extend any superclass.

A basic Model class looks like this:

class User{
    public $id;
    public $uname;
    public $pwd;
    public $group;
    public $vip;

    public $_table = 'user';
    public $_primarykey = 'id';
    public $_fields = array('id', 'uname', 'pwd', 'group', 'vip');
}

With the basic Model class, you can search for a database record by:

//$this->db()->find('User');  is the same
Doo::db()->find('User');

//search for one record
Doo::db()->find('User', array('limit'=>1) );

//search for a user named 'david'
Doo::db()->find('User', array('where'=>"uname='david'", 'limit'=>1) );

//using prepared statement to avoid sql injection
Doo::db()->find('User', array(
                    'where' => 'user=?',
                    'param' => array($_GET['name']),
                    'limit' => 1
                )
           );

//Or simply use this for shorter code.
Doo::loadModel('User');
$u = new User;
$u->uname = $_GET['name'];
$result = Doo::db()->find($u, array('limit'=>1));

Although the above code is pretty straightforward, we can make it even shorter and cleaner. First of all, we would need to have our Models to extend the DooModel class. We will have our Model class as the code below, notice the constructor:

Doo::loadCore('db/DooModel');
class User extends DooModel {
    public $id;
    public $uname;
    public $pwd;
    public $group;
    public $vip;

    public $_table = 'user';
    public $_primarykey = 'id';
    public $_fields = array('id', 'uname', 'pwd', 'group', 'vip');

    function __construct(){
         parent::$className = __CLASS__;
    }
}

By

More »

DooPHP Sitemap Generator Tool Demo

This is a demo showing you how to use DooPHP sitemap generator tools to generate routes as well as Controller classes.

DooPHP Sitemap Generator tool

More »

Quick guide to DooSession

This will be a quick guide how to use DooSession, first of all you need to call a DooSession class, I love to have one super class that extends DooController class. In super class constructor I make session and create everything I need for a website.

Calling session class needs one parameter and thats namespace name, often you wish to put name for your session.

$this->_application = Doo::session("mywebsite");

Now that you created session namespace you can start using it, storing in session and getting stuff from it. For example you want to define username of user that is registered:

$this->_application->user = "John";

Now you are accessing it from controller with:

$this->_application->user

Another nice thing is to have access to session from your view scripts, I am doing it like this, inside constructor of my super class I add:

$data['application'] = Doo::session("mywebsite");
$this->renderc('templatefile', $data);

Now from your view scripts you can access your user variable like:

$this->data['application']->user

Thats about it, all functions that you have in doo session class are in API.

http://doophp.com/documentation/api_svn

More »