PHP Classes

Working with CodeIgniter HMVC Framework, Smarty Template and Doctrine ORM all together

Recommend this page to a friend!
  Blog PHP Classes blog   RSS 1.0 feed RSS 2.0 feed   Blog Working with CodeIgni...   Post a comment Post a comment   See comments See comments (35)   Trackbacks (0)  

Author:

Viewers: 2,568

Last month viewers: 107

Categories: PHP Tutorials

This is a tutorial to show how to combine CodeIgniter HMVC Framework with Smarty Template and Doctrine ORM.

Read this article to learn how we can use various ORM commands in your Web sites without the need to use command line tools.

The article presents a sample project that you can use as a starting point for your own PHP projects.




Loaded Article

Contents

Getting Started

Converting to use CodeIgniter with HMVC Pattern using Modular Extensions

Conclusion


Getting Started

In this tutorial we will combine the following packages together:

1.  CodeIgniter 2.2.0

2.  CodeIgniter Modular Extensions

3.  Smarty 3.1.21

4.  Doctrine 2.2.3

You need to download this ORM package.

Start by downloading CodeIgniter 2.2.0. Extract it into your web server's www folder. Browse to /index.php/welcome. You should see CodeIgniter Welcome Page.

CI Configuration

1.  Open /application/config/config.php in a editor of your choice.
2.  Remove index.php from Line #29. After editing the line should look like as follows:

     $config['index_page'] = '';

3.  At line #227 of config file set a random encryption key.
4.  At line #344 of config file change rewrite_short_tags from FALSE to TRUE. After editing the line should look like as follows:

     $config['rewrite_short_tags'] = TRUE;

5.  Save & Close the file.
6.  Create a new file under your project's root folder named .htaccess and write the following into this file:


 php_flag display_errors off  AddType text/x-component .htc  <IfModule mod_rewrite.c>   DirectoryIndex index.php   RewriteEngine On   RewriteCond $1 !^(index\.php|assets|robots\.txt|favicon\.ico)   RewriteCond %{REQUEST_FILENAME} !-f   RewriteCond %{REQUEST_FILENAME} !-d   RewriteRule ^(.*)$ index.php/$1 [L]  </IfModule>  <IfModule !mod_rewrite.c>   ErrorDocument 404 /index.php  </IfModule>  # BEGIN REQUIRED FOR WEBFONTS  AddType font/ttf .ttf  AddType font/eot .eot  AddType font/otf .otf  AddType font/woff .woff  <FilesMatch "\.(ttf|otf|eot|woff)$">  <IfModule mod_headers.c>   Header set Access-Control-Allow-Origin "*"  </IfModule>  </FilesMatch>  # END REQUIRED FOR WEBFONTS

7.  Browse to /welcome and now you should see CI Welcome page.

Converting to use CodeIgniter with HMVC Pattern using Modular Extensions

HMVC makes the CodeIgniter PHP framework modular. Modules are groups of independent components, typically model, controller and view, arranged in an application modules sub-directory that can be dropped into other CodeIgniter applications.

HMVC stands for Hierarchical Model View Controller. Module controllers can be used as normal controllers or HMVC controllers and they can be used as widgets to help you build view partials.

Installation:

1.  Download and extract CI Modular Extensions from the link above.
2.  Drop Modular Extensions third_party files into your application/third_party directory
3.  Drop Modular Extensions core files into application/core.
4.  Access the URL /welcome => shows Welcome to CodeIgniter
5.  Create module directory structure application/modules/welcome/controllers
6.  Move controller application/controllers/welcome.php to application/modules/welcome/controllers/welcome.php
7.  Access the URL /welcome => shows Welcome to CodeIgniter
8.  Create directory application/modules/welcome/views
9.  Move view application/views/welcome_message.php to application/modules/welcome/views/welcome_message.php
10.  Access the URL /welcome => shows Welcome to CodeIgniter

If you see welcome page correctly then you successfully installed HMVC Pattern into CI.

Installing Smarty

Smarty is a template engine for PHP, facilitating the separation of presentation (HTML/CSS) from application logic. This implies that PHP code is application logic, and is separated from the presentation.

Under the hood, Smarty compiles copies of the templates as PHP scripts. This way you get the benefits of both template tag syntax and the speed of PHP.

Compilation happens once when each template is first invoked, and then the compiled versions are used from that point forward. Smarty takes care of this for you, so the template designer just edits the Smarty templates and never has to manage the compiled versions.

This approach keeps the templates easy to maintain, and yet keeps execution times extremely fast. Since the compiled versions are PHP, op-code accelerators such as APC or ZendCache continue to work on the compiled scripts.

Installation:

1.  Download and extract Smarty from the above link.
2.  Create smarty directory structure /application/third_party/Smarty
3.  Copy files from libs folder of the extracted folder to /application/third_party/Smarty folder.
4.  Create directory structure /application/cache/smarty/cache
5.  Create directory structure /application/cache/smarty/compiled
6.  Make sure above folders both have write access.
7.  Create and open a file in /application/core/MY_Output.php and fill it with the following content:


<?php (defined('BASEPATH')) OR exit('No direct script access allowed');
/**
 * Turn Smarty debug compatible whith Ci-Smarty parse fetch method
 *
 * Responsible for sending debug Smarty final output to browser (Smarty_Internal_Debug::display_debug) 
 * using debug console (pop-window)
 * (tks for Redn0x - http://www.smarty.net/docs/en/chapter.debugging.console.tpl)
 *
 * @category Output
 * @package  CodeIgniter
 * @author   Tariqul Islam <tareq@webkutir.net>
 * @license  http://directory.fsf.org/wiki/License:ReciprocalPLv1.3 Reciprocal Public License v1.3
 * @link     http://webkutir.net
*/
class MY_Output extends CI_Output
{
    /**
     * _display
     *
     * Turn Smarty debug compatible whith Ci-Smarty parse fetch method
     *
     * @param string $output output of the method
     *
     * @return void
     */
    function _display($output = '')
    {
        parent::_display($output);
        //If Smarty is active - NOTE: $this->output->enable_profiler(TRUE) active Smarty debug to simplify
        if (class_exists('CI_Controller') 
            && class_exists('Smarty_Internal_Debug') 
            && (config_item('smarty_debug') || $this->enable_profiler)
        ) {
            $CI =& get_instance();
            Smarty_Internal_Debug::display_debug($CI->smarty);
        }
    }
}
// END MY_Output Class
/* End of file MY_Output.php */
/* Location: ./application/core/MY_Output.php */


8.  Save and close the file. This file will be used to show debug information about Smarty when Smarty debug is set to on.
9.  Create and open a file in /application/config/smarty.php and fill it with the following content:

<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
/**
 * CI Smarty
 *
 * Smarty templating for Codeigniter
 *
 * @package   CI Smarty
 * @author    Dwayne Charrington
 * @copyright Copyright (c) 2012 Dwayne Charrington and Github contributors
 * @link      http://ilikekillnerds.com
 * @license   http://www.apache.org/licenses/LICENSE-2.0.html
 * @version   2.0
 */
// Smarty caching enabled by default unless explicitly set to FALSE
$config['cache_status']         = TRUE;
// The path to the themes
// Default is implied root directory/themes/
$config['theme_path'] = 'themes/';
// The default name of the theme to use (this can be overridden)
$config['theme_name'] = "default";
// Cache lifetime. Default value is 3600 seconds (1 hour) Smarty's default value
$config['cache_lifetime']       = 3600;
// Where templates are compiled
$config['compile_directory']    = APPPATH."cache/smarty/compiled/";
// Where templates are cached
$config['cache_directory']      = APPPATH."cache/smarty/cache/";
// Where Smarty configs are located
$config['config_directory']     = APPPATH."third_party/Smarty/configs/";
// Where my plugins are located
$config['plugins_directory']     = APPPATH."third_party/Smarty/myplugins/";
// Default extension of templates if one isn't supplied
$config['template_ext']         = 'tpl';
// Error reporting level to use while processing templates
$config['template_error_reporting'] = E_ALL & ~E_NOTICE;
// Debug mode turned on or off (TRUE / FALSE)
$config['smarty_debug'] = FALSE;

10.  Save and close the file. This file will be used to configure Smarty in CI. Set smarty debug to TRUE at line #48 if you need to enable debug.
11. Create and open a file in /application/libraries/smarty.php and fill it with the following content:


<?php (defined('BASEPATH')) OR exit('No direct script access allowed');
require_once APPPATH."third_party/Smarty/SmartyBC.class.php";

/**
 * CI Smarty
 *
 * Smarty templating for Codeigniter
 *
 * @category CodeIgniter
 * @package  CI_Smarty
 * @author   Dwayne Charrington <email@email.com>
 * @license  http://www.apache.org/licenses/LICENSE-2.0.html Apache License
 * @link     http://ilikekillnerds.com
 */
class CI_Smarty extends SmartyBC
{
    public $template_ext = '.php';
    /**
     * Class Constructor
     */
    public function __construct()
    {
        parent::__construct();
        // Store the Codeigniter super global instance... whatever
        $CI = get_instance();
        // Load the Smarty config file
        $CI->load->config('smarty');
        // Turn on/off debug
        $this->debugging = config_item('smarty_debug');
        // Set some pretty standard Smarty directories
        $this->setCompileDir(config_item('compile_directory'));
        $this->setCacheDir(config_item('cache_directory'));
        $this->setConfigDir(config_item('config_directory'));
        $this->addPluginsDir(config_item('plugins_directory'));
        // Default template extension
        $this->template_ext = config_item('template_ext');
        $this->force_compile = 1;
        // How long to cache templates for
        $this->cache_lifetime = config_item('cache_lifetime');
        // Disable Smarty security policy
        $this->disableSecurity();
        // If caching is enabled, then disable force compile and enable cache
        if (config_item('cache_status') === true) {
            $this->enable_caching();
        } else {
            $this->disable_caching();
        }
        // Set the error reporting level
        $this->error_reporting   = config_item('template_error_reporting');
        // This will fix various issues like filemtime errors that some people experience
        // The cause of this is most likely setting the error_reporting value above
        // This is a static function in the main Smarty class
        //Smarty::muteExpectedErrors();
        // Should let us access Codeigniter stuff in views
        // This means we can go for example {$this->session->userdata('item')}
        // just like we normally would in standard CI views
        $this->assign("this", $CI);
        //My Vars
        $this->assign('APPPATH', APPPATH);
        $this->assign('BASEPATH', BASEPATH);
        $this->assign('systemurl', base_url());
        $this->assign('template', 'default');
        $this->assign("CI", $CI);
    }
    /**
     * Enable Caching
     *
     * Allows you to enable caching on a page by page basis
     * 
     * @example $this->smarty->enable_caching(); then do your parse call
     * 
     * @return void
     */
    public function enable_caching()
    {
        $this->caching = 1;
    }
    /**
     * Disable Caching
     *
     * Allows you to disable caching on a page by page basis
     * 
     * @example $this->smarty->disable_caching(); then do your parse call
     * 
     * @return void
     */
    public function disable_caching()
    {
        $this->caching = 0; 
    }
}

12.  Save and close the file.
13.  Create and open a file in /application/helpers/parser_helper.php and fill it with the following content:

<?php (defined('BASEPATH')) OR exit('No direct script access allowed');
/**
 * CI Smarty
 *
 * Smarty templating for Codeigniter
 *
 * @package   CI Smarty
 * @author    Dwayne Charrington
 * @copyright Copyright (c) 2012 Dwayne Charrington and Github contributors
 * @link      http://ilikekillnerds.com
 * @license   http://www.apache.org/licenses/LICENSE-2.0.html
 * @version   2.0
 */
/**
 * Theme URL
 *
 * A helper function for getting the current theme URL
 * in a web friendly format.
 *
 * @param string $location Theme Location
 * 
 * @return mixed
 */
function theme_url($location = '')
{
    $CI =& get_instance();
    return $CI->parser->theme_url($location);
}
/**
 * CSS
 *
 * A helper function for getting the current theme CSS embed code
 * in a web friendly format
 *
 * @param string $file       File Name
 * @param array  $attributes Array of the Attributes
 * 
 * @return void
 */
function css($file, $attributes = array())
{
    $CI =& get_instance();
    echo $CI->parser->css($file, $attributes);
}
/**
 * JS
 *
 * A helper function for getting the current theme JS embed code
 * in a web friendly format
 *
 * @param string $file       File Name
 * @param array  $attributes Array of the Attributes
 * 
 * @return void
 */
function js($file, $attributes = array())
{
    $CI =& get_instance();
    echo $CI->parser->js($file, $attributes);
}
/**
 * IMG
 *
 * A helper function for getting the current theme IMG embed code
 * in a web friendly format
 *
 * @param string $file       File Name
 * @param array  $attributes Array of the Attributes
 * 
 * @return void
 */
function img($file, $attributes = array())
{
    $CI =& get_instance();
    echo $CI->parser->img($file, $attributes);
}

14.  Save and close the file.
15.  Create and open a file in /application/libraries/MY_Parser.php and fill it with the following content:

<?php (defined('BASEPATH')) OR exit('No direct script access allowed');
/**
 * CI Smarty
 *
 * Smarty templating for Codeigniter
 *
 * @category CodeIgniter
 * @package  CI_Smarty
 * @author   Dwayne Charrington <email@email.com>
 * @license  http://www.apache.org/licenses/LICENSE-2.0.html Apache License Version 2.0
 * @link     http://ilikekillnerds.com
 */
class MY_Parser extends CI_Parser
{
    protected $CI;
    protected $_module = '';
    protected $_template_locations = array();
    // Current theme location
    protected $_current_path = null;
    // The name of the theme in use
    protected $_theme_name = '';
    /**
     * Class Constructor
     */
    public function __construct()
    {
        // Codeigniter instance and other required libraries/files
        $this->CI =& get_instance();
        //$this->CI->load->library('smarty');
        $this->CI->load->helper('parser');
        // Detect if we have a current module
        if ($this->_module=='') {
            $this->_module = $this->current_module();
        }
        // What controllers or methods are in use
        $this->_controller  = $this->CI->router->fetch_class();
        $this->_method      = $this->CI->router->fetch_method();
        // If we don't have a theme name stored
        if ($this->_theme_name == '') {
            $this->set_theme(config_item('theme_name'));
        }
        // Update theme paths
        $this->_update_theme_paths();
    }
    /**
     * Set the Module
     *
     * @param string $module Name of the Module to set
     *
     * @return void
     */
    public function setModule($module)
    {
        $this->_module=$module;
        $this->_update_theme_paths();
    }
    /**
     * Get the Module
     *
     * @return void
     */
    public function getModule()
    {
        return $this->_module;
    }
    /**
     * Call
     * able to call native Smarty methods
     *
     * @param string $method Name of Method
     * @param array  $params Array of the Params
     *
     * @return mixed
     */
    public function __call($method, $params=array())
    {
        if (!method_exists($this, $method)) {
            return call_user_func_array(array($this->CI->smarty, $method), $params);
        }
    }
    /**
     * Set Theme
     *
     * Set the theme to use
     *
     * @param string $name Name of the Theme to set
     *
     * @return string
     */
    public function set_theme($name)
    {
        // Store the theme name
        $this->_theme_name = trim($name);
        // Our themes can have a functions.php file just like Wordpress
        $functions_file  = config_item('theme_path') . $this->_theme_name . '/functions.php';
        // Incase we have a theme in the application directory
        $functions_file2 = APPPATH."themes/" . $this->_theme_name . '/functions.php';
        // If we have a functions file, include it
        if (file_exists($functions_file)) {
            include_once $functions_file;
        } elseif (file_exists($functions_file2)) {
            include_once $functions_file2;
        }
        // Update theme paths
        $this->_update_theme_paths();
    }
    /**
     * Get Theme
     *
     * Does what the function name implies: gets the name of
     * the currently in use theme.
     *
     * @return string
     */
    public function get_theme()
    {
        return (isset($this->_theme_name)) ? $this->_theme_name : '';
    }
    /**
     * Current Module
     *
     * Just a fancier way of getting the current module
     * if we have support for modules
     *
     * @return string
     */
    public function current_module()
    {
        // Modular Separation / Modular Extensions has been detected
        if (method_exists($this->CI->router, 'fetch_module')) {
            $module = $this->CI->router->fetch_module();
            return (!empty($module)) ? $module : '';
        } else {
            return '';
        }
    }
    /**
     * Parse
     *
     * Parses a template using Smarty 3 engine
     *
     * @param string  $template Name of the Template
     * @param array   $data     Array of Data
     * @param boolean $return   Indicates if it return the view or not
     * @param boolean $caching  Indicates if caching is enabled or not
     * @param string  $theme    Theme Name
     *
     * @return string
     */
    public function parse($template, $data = array(), $return = false, $caching = true, $theme = '')
    {
        // Check the Permission and read dir if necessary
        /*
        if (is_array($this->CI->session->userdata('permission'))
            && in_array("all", $this->CI->session->userdata('permission'))
        ) {
            $this->CI->load->helper('file');
            $data['files']=get_filenames(APPPATH.'views/default/navigation/');
            sort($data['files']);
        }
        */
        // If we don't want caching, disable it
        if ($caching === false) {
            $this->CI->smarty->disable_caching();
        }
        // If no file extension dot has been found default to defined extension for view extensions
        if ( ! stripos($template, '.')) {
            $template = $template.".".$this->CI->smarty->template_ext;
        }
        // Are we overriding the theme on a per load view basis?
        if ($theme !== '') {
            $this->set_theme($theme);
        }
        // Get the location of our view, where the hell is it?
        // But only if we're not accessing a smart resource
        if ( ! stripos($template, ':')) {
            $template = $this->_find_view($template);
        }
        // If we have variables to assign, lets assign them
        if ( ! empty($data)) {
            foreach ($data AS $key => $val) {
                $this->CI->smarty->assign($key, $val);
            }
        }
        // Load our template into our string for judgement
        $template_string = $this->CI->smarty->fetch($template);
        // If we're returning the templates contents, we're displaying the template
        if ($return === false) {
            $this->CI->output->append_output($template_string);
            return true;
        }
        // We're returning the contents, fo' shizzle
        return $template_string;
    }
    /**
     * CSS
     *
     * An asset function that returns a CSS stylesheet
     *
     * @param string $file       File Name
     * @param array  $attributes Attributes array
     *
     * @return string
     */
    public function css($file, $attributes = array())
    {
        $defaults = array(
                'media' => 'screen',
                'rel'   => 'stylesheet',
                'type'  => 'text/css'
        );
        $attributes = array_merge($defaults, $attributes);
        $return  = '<link rel="'.$attributes['rel'].'" type="'.$attributes['type'].'" ';
        $return .= 'href="'.base_url(config_item('theme_path').$this->get_theme()."/css/".$file);
        $return .= '" media="'.$attributes['media'].'">';
        return $return;
    }
    /**
     * JS
     *
     * An asset function that returns a script embed tag
     *
     * @param string $file       File Name
     * @param array  $attributes Attributes array
     *
     * @return string
     */
    public function js($file, $attributes = array())
    {
        $defaults = array(
                'type'  => 'text/javascript'
        );
        $attributes = array_merge($defaults, $attributes);
        $return = '<script type="'.$attributes['type'].'" src="'.
                base_url(config_item('theme_path').$this->get_theme()."/js/".$file).'"></script>';
        return $return;
    }
    /**
     * IMG
     *
     * An asset function that returns an image tag
     *
     * @param string $file       File Name
     * @param array  $attributes Attributes array
     *
     * @return string
     */
    public function img($file, $attributes = array())
    {
        $defaults = array(
                'alt'    => '',
                'title'  => ''
        );
        $attributes = array_merge($defaults, $attributes);
        $return = '<img src ="'.base_url(config_item('theme_path').$this->get_theme()."/img/".$file).
        '" alt="'.$attributes['alt'].'" title="'.$attributes['title'].'" />';
        return $return;
    }
    /**
     * Theme URL
     *
     * A web friendly URL for determining the current
     * theme root location.
     *
     * @param string $location Theme Location
     *
     * @return string
     */
    public function theme_url($location = '')
    {
        // The path to return
        $return = base_url(config_item('theme_path').$this->get_theme())."/";
        // If we want to add something to the end of the theme URL
        if ($location !== '') {
            $return = $return.$location;
        }
        return trim($return);
    }
    /**
     * Find View
     *
     * Searches through module and view folders looking for your view, sir.
     *
     * @param string $file File Name
     *
     * @return string The path and file found
     */
    protected function _find_view($file)
    {
        // We have no path by default
        $path = null;
        // Iterate over our saved locations and find the file
        foreach ($this->_template_locations AS $location) {
            if (file_exists($location.$file)) {
                // Store the file to load
                $path = $location.$file;
                $this->_current_path = $location;
                // Stop the loop, we found our file
                break;
            }
        }
        // Return the path
        return $path;
    }
    /**
     * Add Paths
     *
     * Traverses all added template locations and adds them
     * to Smarty so we can extend and include view files
     * correctly from a slew of different locations including
     * modules if we support them.
     *
     * @return void
     */
    protected function _add_paths()
    {
        // Iterate over our saved locations and find the file
        foreach ($this->_template_locations AS $location) {
            $this->CI->smarty->addTemplateDir($location);
        }
    }
    /**
     * Update Theme Paths
     *
     * Adds in the required locations for themes
     *
     * @return void
     */
    protected function _update_theme_paths()
    {
        // Store a whole heap of template locations
        $this->_template_locations = array(
                config_item('theme_path') . $this->_theme_name . '/views/modules/' . $this->_module .'/layouts/',
                config_item('theme_path') . $this->_theme_name . '/views/modules/' . $this->_module .'/',
                config_item('theme_path') . $this->_theme_name . '/views/layouts/',
                config_item('theme_path') . $this->_theme_name . '/views/',
                APPPATH . 'modules/' . $this->_module . '/views/layouts/',
                APPPATH . 'modules/' . $this->_module . '/views/' . $this->_theme_name . '/',
                APPPATH . 'modules/' . $this->_module . '/views/',
                APPPATH . 'views/layouts/',
                APPPATH . 'views/',
                APPPATH . 'views/' . $this->_theme_name . '/'
        );
        // Will add paths into Smarty for "smarter" inheritance and inclusion
        $this->_add_paths();
    }
    /**
     * String Parse
     *
     * Parses a string using Smarty 3
     *
     * @param string  $template   Template Name
     * @param array   $data       Data Array
     * @param boolean $return     Indicates if it will be returned or not
     * @param boolean $is_include Indicates if included or not
     *
     * @return void
     */
    public function string_parse($template, $data = array(), $return = false, $is_include = false)
    {
        return $this->CI->smarty->fetch('string:'.$template, $data);
    }
    /**
     * Parse String
     *
     * Parses a string using Smarty 3. Never understood why there
     * was two identical functions in Codeigniter that did the same.
     *
     * @param string  $template   Template Name
     * @param array   $data       Data Array
     * @param boolean $return     Indicates if it will be returned or not
     * @param boolean $is_include Indicates if included or not
     *
     * @return void
     */
    public function parse_string($template, $data = array(), $return = false, $is_include = false)
    {
        return $this->string_parse($template, $data, $return, $is_include);
    }
}

16. Save  and close the file.
17.  Open /application/config/autoload.php file and edit Line #55 as follows:

$autoload['libraries'] = array('smarty', 'parser');

18.  and Line #67 as follows:

$autoload['helper'] = array('url');

19.  Save and close the file.

20.  Replace the content of /application/modules/welcome/controllers/welcome.php file with the following:


<?php (defined('BASEPATH')) OR exit('No direct script access allowed');
/**
 * Welcome Controller
 *
 * @category Controller
 * @package  CodeIgniter
 * @author   Tariqul Islam <tareq@webkutir.net>
 * @license  http://directory.fsf.org/wiki/License:ReciprocalPLv1.3 Reciprocal Public License v1.3
 * @link     http://webkutir.net
 */
class Welcome extends MX_Controller
{
    /**
     * Class Constructor
     */
    function __construct()
    {
        parent::__construct();
        $this->parser->setModule("welcome");
    }
    /**
     * Default Method
     * 
     * @return void
     */
    public function index()
    {
        $this->parser->parse("welcome_message");
    }
}
/* End of file welcome.php */
/* Location: ./application/modules/welcome/controllers/welcome.php */

21.  Delete your /application/modules/welcome/views/welcome_message.php file.
22.  Create a new file in /application/modules/welcome/views/welcome_message.tpl with the following content:

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Welcome to CodeIgniter</title>
<style type="text/css">
::selection{ background-color: #E13300; color: white; }
::moz-selection{ background-color: #E13300; color: white; }
::webkit-selection{ background-color: #E13300; color: white; }
body {
background-color: #fff;
margin: 40px;
font: 13px/20px normal Helvetica, Arial, sans-serif;
color: #4F5155;
}
a {
color: #003399;
background-color: transparent;
font-weight: normal;
}
h1 {
color: #444;
background-color: transparent;
border-bottom: 1px solid #D0D0D0;
font-size: 19px;
font-weight: normal;
margin: 0 0 14px 0;
padding: 14px 15px 10px 15px;
}
code {
font-family: Consolas, Monaco, Courier New, Courier, monospace;
font-size: 12px;
background-color: #f9f9f9;
border: 1px solid #D0D0D0;
color: #002166;
display: block;
margin: 14px 0 14px 0;
padding: 12px 10px 12px 10px;
}
#body{
margin: 0 15px 0 15px;
}

p.footer{
text-align: right;
font-size: 11px;
border-top: 1px solid #D0D0D0;
line-height: 32px;
padding: 0 10px 0 10px;
margin: 20px 0 0 0;
}

#container{
margin: 10px;
border: 1px solid #D0D0D0;
-webkit-box-shadow: 0 0 8px #D0D0D0;
}
</style>
</head>
<body>
<div id="container">
<h1>Welcome to CodeIgniter!</h1>
<div id="body">
<p>The page you are looking at is being generated dynamically by CodeIgniter.</p>
<p>If you would like to edit this page you'll find it located at:</p>
<code>application/views/welcome_message.php</code>
<p>The corresponding controller for this page is found at:</p>
<code>application/controllers/welcome.php</code>
<p>If you are exploring CodeIgniter for the very first time, you should start by reading the <a href="user_guide/">User Guide</a>.</p>
</div>
<p class="footer">Page rendered in <strong>{$CI->benchmark->elapsed_time()}</strong> seconds</p>
</div>
</body>
</html>

23.  Access the URL /welcome => shows Welcome to CodeIgniter. Notice the Page Render time. For my case it was 7.5080 seconds for the first time.
24.  Again access the URL /welcome. Now notice the Page Render time. For my case it was 0.1046 seconds for the second time, 0.0794 seconds for the third time.

Note: If you see white blank page or any error then you should check folder permissions of /application/cache/smarty/cache and /application/cache/smarty/compiled directories.

Congratulations!!! Your Smarty is working with your CI successfully.

Installing Doctrine

1.  Download and extract Doctrine using the link above.
2.  Copy Doctrine folder into /application/third_party folder.
3.  Create a file in /application/libraries/Doctrine.php and fill it with the following:

<?php (defined('BASEPATH')) OR exit('No direct script access allowed');
use Doctrine\ORM\EntityManager,
    Doctrine\ORM\Configuration,
    Doctrine\ORM,
    Doctrine\Common\Cache\ArrayCache,
    Doctrine\OXM;
 
define('DEBUGGING', false);
/**
 * Doctrine
 *
 * @category Libraries
 * @package  CodeIgniter
 * @author   Tariqul Islam <tareq@webkutir.net>
 * @license  http://directory.fsf.org/wiki/License:ReciprocalPLv1.3 Reciprocal Public License v1.3
 * @link     http://webkutir.net
 */
class Doctrine
{
    public $em = null;
    /**
     * Class Constructor
     */
    public function __construct()
    {
        // load database configuration and custom config from CodeIgniter
        include APPPATH . 'config/database.php';
        // Set up class loading.
        include_once APPPATH . 'third_party/Doctrine/Common/ClassLoader.php';
        $doctrineClassLoader = new \Doctrine\Common\ClassLoader('Doctrine', APPPATH . 'third_party');
        $doctrineClassLoader->register();
        $entitiesClassLoader = new \Doctrine\Common\ClassLoader('entities', APPPATH . 'models/doctrine');
        $entitiesClassLoader->register();
        $proxiesClassLoader = new \Doctrine\Common\ClassLoader('proxies', APPPATH . 'cache/doctrine');
        $proxiesClassLoader->register();
        $symfonyClassLoader = new \Doctrine\Common\ClassLoader('Symfony', APPPATH . 'third_party/Doctrine');
        $symfonyClassLoader->register();
        // Choose caching method based on application mode (ENVIRONMENT is defined in /index.php)
        if (ENVIRONMENT == 'development' || ENVIRONMENT == 'testing') {
            $cache = new \Doctrine\Common\Cache\ArrayCache;
        } else {
            $cache = new \Doctrine\Common\Cache\ApcCache;
        }
        // Set some configuration options
        $config = new Configuration;
        // Metadata driver
        $driverImpl = $config->newDefaultAnnotationDriver(APPPATH . 'models/doctrine/entities');
        $config->setMetadataDriverImpl($driverImpl);
        // Caching
        $config->setMetadataCacheImpl($cache);
        $config->setQueryCacheImpl($cache);
        // Proxies
        $config->setProxyDir(APPPATH . 'cache/doctrine/proxies');
        $config->setProxyNamespace('Proxies');
        if (ENVIRONMENT == 'development') {
            $config->setAutoGenerateProxyClasses(true);
        } else {
            $config->setAutoGenerateProxyClasses(false);
        }
        // SQL query logger
        if (DEBUGGING) {
            $logger = new \Doctrine\DBAL\Logging\EchoSQLLogger;
            $config->setSQLLogger($logger);
        }
        // Database connection information
        if (ENVIRONMENT == 'testing') {
            $active_group = 'default_test';
        }
        $connectionOptions = array(
            'driver'   => 'pdo_mysql',
            'user'     => $db[$active_group]['username'],
            'password' => $db[$active_group]['password'],
            'host'     => $db[$active_group]['hostname'],
            'dbname'   => $db[$active_group]['database']
        );
        // Create EntityManager
        $this->em = EntityManager::create($connectionOptions, $config);
    }
}

4.  Create the following folder structures:
/application/models/doctrine/entities
/application/cache/doctrine/generated_entities (give write permission)
/application/cache/doctrine/proxies (give write permission)

5.  Open the /application/config/autoload.php file and modify it as follows:

$autoload['libraries'] = array('smarty', 'parser', 'doctrine');

6.  Create a file in /application/core/MY_Controller.php and fill it with the following:


<?php (defined('BASEPATH')) OR exit('No direct script access allowed');
/**
 * MY_Controller
 *
 * @category MY_Controller
 * @package  CodeIgniter
 * @author   Tariqul Islam <tareq@webkutir.net>
 * @license  http://directory.fsf.org/wiki/License:ReciprocalPLv1.3 Reciprocal Public License v1.3
 * @link     http://webkutir.net
 */
class MY_Controller extends MX_Controller
{
    public $em;
    /**
     * Constructor of MY Controller
     */
    function __construct()
    {
        parent::__construct();
        $this->em=$this->doctrine->em;
        $this->smarty->assign('currenturl', $this->uri->uri_string());
    }
}

7.  From now on all of your controllers in all modules should extend MY_Controller.
8.  Access the URL /welcome => shows Welcome to CodeIgniter
9.  Now we need to define Doctrine Models. We will build our Doctrine Models using AnnotationDriver which is very simple to use.
10.  Create /application/models/doctrine/entities/User.php file with following:


<?php

namespace entities;
/**
 * User
 *
 * @Table(name="user")
 * @Entity
 */
class User
{
  /**
   * @var integer
   *
   * @Column(name="id", type="bigint", nullable=false)
   * @Id
   * @GeneratedValue(strategy="IDENTITY")
   */
  private $id;
  /**
   * @var string
   *
   * @Column(name="username", type="string", length=100, nullable=false)
   */
  private $username;
  /**
   * @var string
   *
   * @Column(name="email", type="string", length=150, nullable=false)
   */
  private $email;
  /**
   * @var string
   *
   * @Column(name="password", type="string", length=40, nullable=true)
   */
  private $password;
  /**
   * @var string
   *
   * @Column(name="first_name", type="string", length=100, nullable=false)
   */
  private $firstName;
  /**
   * @var string
   *
   * @Column(name="last_name", type="string", length=100, nullable=true)
   */
  private $lastName;
  /**
   * @var string
   *
   * @Column(name="mobile", type="string", length=15, nullable=true)
   */
  private $mobile;
  /**
   * @var string
   *
   * @Column(name="timezone", type="string", length=6, nullable=false)
   */
  private $timezone;
  /**
   * @var string
   *
   * @Column(name="validation_key", type="string", length=40, nullable=true)
   */
  private $validationKey;
  /**
   * @var string
   *
   * @Column(name="image_path", type="string", length=255, nullable=true)
   */
  private $imagePath;
  /**
   * @var string
   *
   * @Column(name="logo_path", type="string", length=500, nullable=true)
   */
  private $logoPath;
  /**
   * @var DateTime
   *
   * @Column(name="last_log_in", type="datetime", nullable=true)
   */
  private $lastLogIn;
  /**
   * @var string
   *
   * @Column(name="ip_address", type="string", length=15, nullable=true)
   */
  private $ipAddress;
  /**
   * @var string
   *
   * @Column(name="host", type="string", length=500, nullable=true)
   */
  private $host;
  /**
   * @var string
   *
   * @Column(name="phone", type="string", length=50, nullable=true)
   */
  private $phone;
  /**
   * @var string
   *
   * @Column(name="land_phone", type="string", length=50, nullable=true)
   */
  private $landPhone;
  /**
   * @var boolean
   *
   * @Column(name="is_active", type="boolean", nullable=false, options={"default":0})
   */
  private $isActive;
  /**
   * @var User
   *
   * @ManyToOne(targetEntity="User")
   * @JoinColumns({
   *   @JoinColumn(name="parent_id", referencedColumnName="id", onDelete="CASCADE")
   * })
   */
  private $parent;
  /**
   * @var UserType
   *
   * @ManyToOne(targetEntity="UserType")
   * @JoinColumns({
   *   @JoinColumn(name="user_type_id", referencedColumnName="id")
   * })
   */
  private $userType;
  
  /**
   * Constructor
   */
  public function __construct()
  {
      $this->isActive = 0;
      $this->lastLogIn = new \DateTime("now");
  }
  /**
   * Get id
   *
   * @return integer 
   */
  public function getId()
  {
    return $this->id;
  }
  /**
   * Set username
   *
   * @param string $username
   * @return User
   */
  public function setUsername($username)
  {
    $this->username = $username;
  
    return $this;
  }
  /**
   * Get username
   *
   * @return string 
   */
  public function getUsername()
  {
    return $this->username;
  }
  /**
   * Set email
   *
   * @param string $email
   * @return User
   */
  public function setEmail($email)
  {
    $this->email = $email;
  
    return $this;
  }
  /**
   * Get email
   *
   * @return string 
   */
  public function getEmail()
  {
    return $this->email;
  }
  /**
   * Set password
   *
   * @param string $password
   * @return User
   */
  public function setPassword($password)
  {
    $this->password = $password;
  
    return $this;
  }
  /**
   * Get password
   *
   * @return string 
   */
  public function getPassword()
  {
    return $this->password;
  }
  /**
   * Set firstName
   *
   * @param string $firstName
   * @return User
   */
  public function setFirstName($firstName)
  {
    $this->firstName = $firstName;
  
    return $this;
  }
  /**
   * Get firstName
   *
   * @return string 
   */
  public function getFirstName()
  {
    return $this->firstName;
  }
  /**
   * Set lastName
   *
   * @param string $lastName
   * @return User
   */
  public function setLastName($lastName)
  {
    $this->lastName = $lastName;
  
    return $this;
  }
  /**
   * Get lastName
   *
   * @return string 
   */
  public function getLastName()
  {
    return $this->lastName;
  }
  
  /**
   * Get fullName
   *
   * @return string
   */
  public function getFullName()
  {
      return $this->firstName.' '.$this->lastName;
  }
  /**
   * Set mobile
   *
   * @param string $mobile
   * @return User
   */
  public function setMobile($mobile)
  {
    $this->mobile = $mobile;
  
    return $this;
  }
  /**
   * Get mobile
   *
   * @return string 
   */
  public function getMobile()
  {
    return $this->mobile;
  }
  /**
   * Set timezone
   *
   * @param string $timezone
   * @return User
   */
  public function setTimezone($timezone)
  {
    $this->timezone = $timezone;
  
    return $this;
  }
  /**
   * Get timezone
   *
   * @return string 
   */
  public function getTimezone()
  {
    return $this->timezone;
  }
  /**
   * Set validationKey
   *
   * @param string $validationKey
   * @return User
   */
  public function setValidationKey($validationKey)
  {
    $this->validationKey = $validationKey;
  
    return $this;
  }
  /**
   * Get validationKey
   *
   * @return string 
   */
  public function getValidationKey()
  {
    return $this->validationKey;
  }
  /**
   * Set imagePath
   *
   * @param string $imagePath
   * @return User
   */
  public function setImagePath($imagePath)
  {
    $this->imagePath = $imagePath;
  
    return $this;
  }
  /**
   * Get imagePath
   *
   * @return string 
   */
  public function getImagePath()
  {
    return $this->imagePath;
  }
  /**
   * Set logoPath
   *
   * @param string $logoPath
   * @return User
   */
  public function setLogoPath($logoPath)
  {
    $this->logoPath = $logoPath;
  
    return $this;
  }
  /**
   * Get logoPath
   *
   * @return string 
   */
  public function getLogoPath()
  {
    return $this->logoPath;
  }
  /**
   * Set lastLogIn
   *
   * @param DateTime $lastLogIn
   * @return User
   */
  public function setLastLogIn($lastLogIn)
  {
    $this->lastLogIn = $lastLogIn;
  
    return $this;
  }
  /**
   * Get lastLogIn
   *
   * @return DateTime 
   */
  public function getLastLogIn()
  {
    return $this->lastLogIn;
  }
  /**
   * Set ipAddress
   *
   * @param string $ipAddress
   * @return User
   */
  public function setIpAddress($ipAddress)
  {
    $this->ipAddress = $ipAddress;
  
    return $this;
  }
  /**
   * Get ipAddress
   *
   * @return string 
   */
  public function getIpAddress()
  {
    return $this->ipAddress;
  }
  /**
   * Set host
   *
   * @param string $host
   * @return User
   */
  public function setHost($host)
  {
    $this->host = $host;
  
    return $this;
  }
  /**
   * Get host
   *
   * @return string 
   */
  public function getHost()
  {
    return $this->host;
  }
  /**
   * Set phone
   *
   * @param string $phone
   * @return User
   */
  public function setPhone($phone)
  {
    $this->phone = $phone;
  
    return $this;
  }
  /**
   * Get phone
   *
   * @return string 
   */
  public function getPhone()
  {
    return $this->phone;
  }
  /**
   * Set landPhone
   *
   * @param string $landPhone
   * @return User
   */
  public function setLandPhone($landPhone)
  {
    $this->landPhone = $landPhone;
  
    return $this;
  }
  /**
   * Get landPhone
   *
   * @return string 
   */
  public function getLandPhone()
  {
    return $this->landPhone;
  }
  /**
   * Set isActive
   *
   * @param boolean $isActive
   * @return User
   */
  public function setIsActive($isActive)
  {
    $this->isActive = $isActive;
  
    return $this;
  }
  /**
   * Get isActive
   *
   * @return boolean 
   */
  public function getIsActive()
  {
    return $this->isActive;
  }
  /**
   * Set parent
   *
   * @param User $parent
   * @return User
   */
  public function setParent(User $parent = null)
  {
    $this->parent = $parent;
  
    return $this;
  }
  /**
   * Get parent
   *
   * @return User 
   */
  public function getParent()
  {
    return $this->parent;
  }
  /**
   * Set userType
   *
   * @param UserType $userType
   * @return User
   */
  public function setUserType(UserType $userType = null)
  {
    $this->userType = $userType;
  
    return $this;
  }
  /**
   * Get userType
   *
   * @return UserType 
   */
  public function getUserType()
  {
    return $this->userType;
  }
}

11.  Create a file in /application/models/doctrine/entities/UserType.php and fill it with the following:

<?php


namespace entities;
/**
 * UserType
 *
 * @Table(name="user_type")
 * @Entity
 */
class UserType
{
  /**
   * @var integer
   *
   * @Column(name="id", type="smallint", nullable=false)
   * @Id
   * @GeneratedValue(strategy="IDENTITY")
   */
  private $id;
  /**
   * @var string
   *
   * @Column(name="type_name", type="string", length=150, nullable=false)
   */
  private $typeName;

  /**
   * Get id
   *
   * @return integer 
   */
  public function getId()
  {
    return $this->id;
  }
  /**
   * Set typeName
   *
   * @param string $typeName
   * @return UserType
   */
  public function setTypeName($typeName)
  {
    $this->typeName = $typeName;
  
    return $this;
  }
  /**
   * Get typeName
   *
   * @return string 
   */
  public function getTypeName()
  {
    return $this->typeName;
  }
}

12.  We have defined our Doctrine entity models. For a full list of the available annotations and their uses, see the Annotation Reference.

13.  Now its time to create the database schema, update database schema, drop database schema, etc. To use these commands from web browser, we will now write a module in CI.
14.  Create a database in your database server and configure CI accordingly by editing the /application/config/database.php file.
15.  Download this module and extract it. Copy orm folder in /application/modules folder.
16.  Copy assets folder in /assets folder.
17.  Copy generic_helper.php in /application/helpers folder.
18.  Access the URL /orm => will show 7 Buttons.
19.  Click on Create Schema button. When it shows success then check your database in your database server and you will see that you now have two tables in it named user and user_type.
20.  Now its time to populate some default data in our DB. Click Load Fixture Button and check your database tables. Cool. :)

Note: Don't forget to remove orm module in production server. It will be harmful there.

Conclusion

Well that is it. Please feel free to post a comment if you have any questions.



You need to be a registered user or login to post a comment

1,611,040 PHP developers registered to the PHP Classes site.
Be One of Us!

Login Immediately with your account on:



Comments:

5. Clarification on Helper - Pratheepa (2015-11-16 03:52)
Parser... - 1 reply
Read the whole comment and replies

3. Codeigniter 3.0rc3 with Smarty 3.x - Carlos Sanchez (2015-09-22 02:20)
Codeigniter 3.x + Smarty... - 10 replies
Read the whole comment and replies

4. Clarification on some helper functions and theming - fadojutimi temitayo (2015-09-15 14:16)
... i'll like you to clarify some points you made which are not... - 13 replies
Read the whole comment and replies

2. Where is the download - Arun Joseph (2015-02-04 06:07)
Where can I find the download to the orm module?... - 5 replies
Read the whole comment and replies

1. How about CodeIgniter 3? - Jim Parry (2015-02-03 09:42)
Rework blog post (or write another) for CodeIgniter 3.0... - 1 reply
Read the whole comment and replies



  Blog PHP Classes blog   RSS 1.0 feed RSS 2.0 feed   Blog Working with CodeIgni...   Post a comment Post a comment   See comments See comments (35)   Trackbacks (0)