CodeIgniter Framework Overview

Hello everyone dear habrahabr. This article will focus on the wonderful PHP CodeIgniter framework (abbreviated CI). CodeIgniter is a popular open source PHP framework for building rich and secure web applications with MVC architecture. Developed by EllisLab.
Why do everything from scratch and rewrite 1000 lines of code each time? Why spend a lot of time on the concept of native code? You are lucky if your project has few lines of code and it is well documented, but if you have more than 1000 files and the code is ugly scattered in addition, then a small correction can take a lot of time. There is an exit! Sooner or later you will have to use the framework.

So what is a framework?



Framework  (framework, structure) - software that facilitates the development and integration of various components of a large software project. Unlike libraries that combine a set of routines with similar functionality, the framework contains a large number of different libraries for their intended purpose.


Personal experience



When I started developing applications in pure PHP, I spent a lot of time, looking for a bug for a long time, rewriting a lot of lines of code. Later I began to use the functions, but this did not help either. I googled for a very long time and found the perfect solution - using frameworks. At the moment I am using CodeIgniter, which I will talk about now.

Why CodeIgniter?



CodeIgniter is a framework written in PHP. The first public release of CI took place in 2006 (7 years ago). He quickly gained popularity due to its simplicity and speed. At the moment, the current version is  2.1.3. CodeIgniter uses the MVC architecture, which allows you to put everything in order. Below you can see the CI process. Has the support of libraries, helpers, hooks. There is also a built-in caching system.



And so on the benefits of CodeIgniter



Excellent and clear documentation


Indeed, CodeIgniter has very rich and clear documentation, and it is nice to read it. Everything is explained by code examples, which you can simply copy and use at home. Also, the documentation has long been translated into Russian, so it will not be difficult for you to learn CodeIgniter.

Does not require a large amount of resources


CodeIgniter can work on almost all hosting with support for PHP at least version 5.1. Due to its competent structure, CI does not load the system and works very fast. Now I will not compare the speed of work with other frameworks, since holivars are useless.

Very easy to use


Application development on CodeIgniter has a different style than just writing in pure PHP. It will seem to you that you need to learn a lot in order to develop on the framework, but I will please you, CI learns very easily and quickly. You can develop light and medium-sized applications on CodeIgniter, but for more complex projects it is better to choose a different framework.

Page generation speed.


CodeIgniter is very fast. And this is true, and if you do not believe, then you can check it yourself.

Support for a large number of databases


Using its built-in drivers, CodeIgniter can work with different databases, such as: MySQL, PostgreSQL, MSSQL, SQLite, Oracle. There is also a PDO driver, which is extremely convenient. CI implements an ActiveRecord design pattern for working with databases.

A large number of standard libraries and classes


Whatever you want to do, CodeIgniter will find a solution to most of your ideas. I will not list all the libraries, helpers and classes, since there are a lot of them. But I will list which I use: pagination, captcha_helper, form validation, xmlrpc, email, url_helper, security_helper.

Many resources on CI.


On the Internet there are a huge number of sites dedicated to CodeIgniter. So finding a third-party library or finding the answer to your question on CI will not be difficult. Below in the article there will be links to various resources.

Cons CI



Weak caching system


The caching system in CodeIgniter works only with entire pages, but with parts of the page, and this is not very convenient. And this means that you have to write your caching system or fasten ready-made options. Although at this speed, caching is not necessary.

Bad writing style

CodeIgniter allows you to completely abandon the model, which is not good. CI accustoms with a loose style of programming. Class inheritance is also not very developed; in some places it uses procedural code.

There is no registry pattern

Due to the lack of a registry pattern, you have to write a rather stupid thing, "$ CI = & getInstance ();" for accessing libraries to the core of the framework.

Code examples


It takes only a few lines to insert the code into the database.
$insert = array(
'title' => 'Обзор CodeIgniter' ,
'author' => 'Mister Yio' ,
'text' => 'BlaBlaBla'
);//Массив с элементами для записи
/*Это запись равносильна INSERT INTO posts (title, author, text)
VALUES ('Обзор CodeIgniter', 'Mister Yio', 'BlaBlaBla');*/
$this->db->insert('post', $insert);
return $this-db->insert_id();//Возвращает id последней добавленной записи

It’s also easy to get data and pass it on to your mind.
public function getSingl($name){
$this->db->where('name', $name);//Генерирует WHERE name="$name"
$this->db->select('title, author, text');//Генерирует SELECT title,author,text
$query = $this->db->get('posts');//Посылает запрос к базе данных
foreach($query->result_array() as $row){
 $data[] = $row;
 }
$this->load->view('blog',$data);//Вызываем вид и передаем массив с полученными элементами элементами
}

Below is a standard model for working with a database

db->get('posts');//генерирует запрос SELECT * FROM posts
 if($query->num_rows() > 0){
 return $query->result_array();//Возвращаем массив с полученными данными
 }
 }
 /**
 * Select singl post on database
 * @param integer $id
 * @return array
 */
 public function getOne($id){
 if(!is_integer($id))//Если передано не число то возвращаем предупреждение
 return 'Передан неверный идентификатор записи';
 $this->db->where("id",$id);
 $query = $this->db->get('posts');//Генерирует SELECT * FROM posts WHERE id="$id"
 if($query->num_rows() > 0){
 $data = $query->result_array();
 return $data;//Возвращаем массив с полученными данными
 }else{
 show_404();//Если записей не найдено показываем ошибку 404
 }
 }
 /**
 * Insert post on database
 * @param array $data
 * @return interger insert_id
 */
 public function add(array $data){
 $query = $this->db->insert('posts', $data);
 return $this->db->insert_id();//Возвращаем id последний добавленной записи
 }
 /**
 * Delete post on database
 * @param integer $id
 * @return integer
 */
 public function delete($id){
 if(!is_integer($id))
 return 'Передан неверный идентификатор записи';
 $this->db->where('id',$id);
 $this->db->delete('posts');
 return "Запись $id была удалена";
 }
}
?>

Projects based on CodeIgniter


CodeIgniter is based on a large number of cms and scripts. The most famous: MaxSite CMS, ImageCMS, CI-CMS, Blaze. (Someday this list will replenish me)

Also popular now: