Pages

Showing posts with label CodeIgniter. Show all posts
Showing posts with label CodeIgniter. Show all posts

Sunday, April 05, 2009

PHP Frameworks: CodeIgniter

CodeIgniter is a php web development framework... In its own words:
CodeIgniter is a powerful PHP framework with a very small footprint, built for PHP coders who need a simple and elegant toolkit to create full-featured web applications. If you're a developer who lives in the real world of shared hosting accounts and clients with deadlines, and if you're tired of ponderously large and thoroughly undocumented frameworks
Instead of a web development framework I'd rather call it a collections of libraries that help you in developing a web application... I should continue with "easier" but is not actually like that, beacause "easier" for me means in the first place faster... The MVC pattern that tries to cover is actually going with some best practices and that's it... First time I tried CodeIgniter was at the recommendation of a fellow that was using CodeIgniter and was very happy with it... I tried to build a backend application for quote management, but I gave up and used a framework inspired by CakePHP, but quite truncated with a very good ORM... That was an unhappy experience and left CodeIgniter, and started using more and more CakePHP... The main reason I didn't continue with CodeIgniter was that I had to write lots of code... for everything I need I had to write code... And that's not helpful.. And after a year or so, at our company I got a very nice customer that was quite good four our image and decent pay... We already had a name, couple of large (national and multi-national companies) in out portfolio The code of the website was in 2 controllers (8k and 12k code code, yes that's 8.000 and 12.000 lines of code) and 2 views (the container of html code)... Of course CodeIgniter was used... They had no idea what they were doing in there....
class Start extends Controller {

function funcNameHere($paramx=''){
#code here
$data['content_html'] = 'html code here..';
#sql queries and stuff here... no models used...
#loops here to build the content
$this->load->view('main_view', $data);
}
#other 1000 functions here
}
So it's not at all easy to deal with this... But, without some extra-functionalities needed the application was working... Anyway, instead of rewriting the entire app, we just moved to a proper approach only the modules that needed to be changed... Details in another story maybe... This was another proof that using a Web Development Framework does not actually means writing quality code... My conclusions about CodeIgniter so far:
  • is a collection of libraries for developing web applications
  • not a RAD framework
  • too much code needed to be written
  • good documentation
  • lacks AJAX library
  • low complexity and easy to learn
  • weak ORM
So, a beginner that wants to use a framework, can start with CodeIgniter or if you need more control over your application also you can use CodeIgniter... I will be involved in projects that are built with CodeIgniter only if it's a requirement or it's easier than using something else. I prefer CakePHP (web applications development) and Zend Framework (as a toolkit)...

Wednesday, December 31, 2008

CodeIgniter rss powered by Zend Framework Zend_Feed

In a recently project that we had to finish (started by a different company, went for two years and wasn't finished yet) we handled with a lot of legacy code which used CodeIgniter as a web development framework...

Refactoring it is another discussion because it's a nice case study how not to generate html code in your controller (which containes over 12.000 lines of code - it's a right figure, we still have in svn the original file) and the entire application has 2 controllers - 12k lines of code x 2 (one for frontend and one for backend) and 2 views (pretty cool eh?).

Delivering fast results means no time rewrite an entire application... A nice feature we added is rss feeds. For that we used Zend Framework component Zend_Feed embeded in a CodeIgniter controller... Application structure is:

/
/system
/system/application
/system/application/controllers
/system/codeigniter
[...]
/Zend
/Zend/Feed
.htaccess
index.php

We included in the Zend folder only Zend_Feed and dependencies for this job... Tried to use a Package Maker for Zend Framework, but it does not seem to work (did not included EmailValidation and so on...) Setting up include_path: In index.php add the following line:

set_include_path(getcwd().PATH_SEPARATOR.'.');
So when you call require_once 'Zend/something' - will work.
class Rss extends Controller {

function Rss() {   
   parent::Controller();
   $this->load->helper('text');
   $lang = $this->phpsession->get('lang');
   if(empty($lang)){
       $lang = 'ro';
   }
//...
}

/**
* last 10 news
*
*/
function news()
{

   #load up zend feed
   require_once 'Zend/Feed.php';
   $array = array(
       'title'       => 'FEED TITLE HERE', //required
       'link'        => $_SERVER['REQUEST_URI'], //required
       'lastUpdate'  => time(), // optional
       'published'   => time(), //optional
       'charset'     => 'utf8', // required
       'description' => 'COMPANY NAME news feed', //optional
       'author'      => 'COMPANY NAME', //optional
       'email'       => 'office@example.com', //optional
       'webmaster'   => 'office@example.com',
       'copyright'   => 'All rights reserved COMPANY NAME', //optional
       'image'       => 'http://www.example.com/logo.gif', //optional
       'generator'   => 'myZFeed', // optional
       'ttl'         => '60'
   );


   $fields = array('title','short_descr','descr');
   $lang       = $this->phpsession->get('lang');   
   if($lang=='en'){
       //smart piece of code :)
       $sql_select_fields = array_reduce($fields,create_function('$v,$a','$a .= \'_en as \'.$a;$v.=\',\'.$a; return $v;'));
   } else {
       $sql_select_fields = ','.implode(',',$fields);
   }

   //table name, news in Romanian
   $table = 'noutati';
   $limit = 10;   
   $this->db->select('id '.$sql_select_fields.', UNIX_TIMESTAMP(dt) as rss_timestamp');
   $this->db->where('visible',1);
   $this->db->order_by("dt", "desc");
   $query = $this->db->get($table,$limit);
   foreach ($query->result() as $row){

       $array['entries'][]     = array(
           'title'        => $row->title, //required
           'link'         => $this->config->config['base_url'].'/start/news_details/'.$row->id,
           'description'  => word_limiter($row->short_descr, 20),
           'content'      => $row->descr,
           'lastUpdate'   => $row->rss_timestamp
       );
   }

   $rssFeedFromArray = Zend_Feed::importArray($array, 'rss');
   $rssFeedFromArray->send();
}

}
That's it. Now you have a working rss feed in a CodeIgniter application powered by Zend_Feed :) Session is opened to questions :). Note: will not share the 12k lines of code file... Not sure about copyright and stuff....

Monday, December 29, 2008

PHP Framework trends - Zend Framework vs Symfony vs CakePHP vs CodeIgniter

This graph is for the last 12 months Google trends Zend Framework vs Symfony vs CakePHP vs CodeIgniter
zend framework
1.00
symfony
1.14
cakephp
1.35
codeigniter
0.50