Posts

Showing posts with the label codeigniter

CodeIgniter: Add Session in Twig

To add the session variable on your twig template, you have to add the following line in your Twig library or controller. $this->_twig->addGlobal("session", $this->CI->session); Then on your twig template, you can print the session like this {{ session.userdata.username }} Since in CodeIgniter, the session stored by user is usually in the userdata array. Otherwise, you can just simply call the session variable and name {{ session.username }}

Adding template engine Twig on CodeIgniter

Image
1. Download Twig vendor from http://twig.sensiolabs.org/ 2. Extract the files and copy the contents of the Twig-1.x.x\lib to /application/libraries/ Your library should have the Twig folder inside /libraries/Twig 3. Create a Twig.php inside the libraries folder then add the following code: class Twig { private $CI; private $_twig; private $_template_dir; private $_cache_dir; public function __construct($debug = false) { $this->CI =& get_instance(); self::requireTwigAutoloader(); Twig_Autoloader::register(); $this->CI->config->load('twig'); $this->_template_dir = $this->CI->config->item('template_dir'); $this->_cache_dir = $this->CI->config->item('cache_dir'); $loader = new Twig_Loader_Filesystem($this->_template_dir); $this->_twig = new Twig_Environment($loader, array( 'cache' => $this-...

Installing Propel in CodeIgniter

INSTALLING PROPEL IN CODEIGNITER Download CodeIgniter http://ellislab.com/codeigniter Download Propel ORM http://propelorm.org/download.html Inside CodeIgniter application directory, create the following directories: build build/data Note: Anywhere is fine, this is just for organization Unzip Propel ORM, copy the generator directory and rename to propel in application/build/data Copy build.properties-sample from build/data/propel to build/data without the ‘-sample’ Add the following data to build.properties with your own variables propel.project = ci propel.database = mysql propel.database.url = mysql:host=localhost;dbname=demo propel.database.user = root propel.database.password = password Then at the last part of the same file add: propel.project.dir = ${propel.home} propel.output.dir = ${propel.project.dir}/output propel.conf.dir = ${propel.project.dir} propel.sql.dir = ${propel.output.dir}/sql propel.graph.dir = ${propel.output.dir}/graphs propel.omtar.d...

CodeIgniter - Extending Form Validation

Overview 1. Store all form validation in application/config/form_validation.php 2. Create a library to extend form validation for callback functions 3. Run the form validation inside the controller method Step 1: Create form_validation.php Create the file application/config/form_validation.php then add your form validations In my example, I’ll do something like the code below which validates the inputs when a user creates a news article $config = array( 'article' => array( array( 'field' => 'news_title', 'label' => 'Title', 'rules' => 'trim|required|max_length[100]' ), array( 'field' => 'news_body', 'label' => 'Body', 'rules' => 'trim|required' ), array( 'field' => 'news_category', 'label...