CodeIgniter Basics

CodeIgniter is web application framework know as MVC (Model View Controller) PHP framework. There are many MVC framework (of which I find the most usable CakePHP and Phalcon) but definitely CodeIgniter enables the quickest start and lets you quickly built small or medium size web application compared to other MVC frameworks.

The first question which needs to be answered before we touch basics of CodeIgniter is why to use such MVC framework at all? There is OOP PHP programming as such and in fact you don’t need any framework to write whatever is possible to write in PHP language.

The answer is simple – you use PHP frameworks, be it Zend, CakePHP, Phalcon, CodeIgniter or something else for one reason: comfort of application development. MVC framework seriously speeds up application development due to internal business logic where you separate things like:

  • what and how to do (Controller)
  • how it looks, visual user interface (View)
  • put in/put out data (Model)

Obviously, what is written above is a bit simplified, but it shows more or less what is going on with MVC frameworks. The point is to write chunks of code responsible for Model, Controller and View SEPARATELY. It makes your job much easier after initial shock when you enter MVC logic from “standard” OOP PHP programming. This MVC splitting is important especially in more complicated/bigger web applications. It helps you to orientate in hundreds of files and relations between the files. With MVC framework it is much easier to understand business logic behind web application even if it is big.

Except speeding development you usually get ready solutions (libraries, functions, classes) which come with MVC framework. This way some pretty much frequent tasks are easier to implement – the code exists there in framework ready to be used, sometimes only with only slight modifications. For instance, in CodeIgniter when you want o validate password during login you can write something like that:

$this->form_validation->set_rules(‘password’, ‘Password’,’xss_clean|required|min_length[4]|max_length[40]|sha1|callback_password_check’);

This one line of code means that CodeIgniter will clean password field against cross site scripting attack (xss filter), will require (required) password field (‘password’), minimum length of password is 4 characters and maximum 40 characters (min_length, max_length), password will be encrypted via sha1 encryption method (sha1) and actual check if password matches database records will be done in callback function called password_check (callback_password_check). In password_check function you will need to write code needed for actual check if such password matches username in database. All this requires only several lines of PHP code, because most of the job is already done by CodeIgniter models and libraries provided by this network.

Another nice feature of CodeIgniter is fact that it is rather well documented and installation process is very simple and very fast. All you need are three folders:

  • application folder
  • system folder
  • folder where your application will be working

Don’t be confused with names: application folder of CodeIgniter with comes with CodeIgniter and folder where you want to install your web application. To avoid confusion I will call the folders which are part of CodeIgniter: CI application and CI system. Default folder names for all original files of CodeIgniter are: “application” and “system” and they can be renamed. The folder where your application will work has to have index.php file with information about location of CI application and CI system folders. The both CodeIgniter folders can be located somewhere in root directory (available via browser) of your website or below root directory making them inaccessible via browser, however they are secured anyway even if placed in doc directory. In your web application folder there are only two files: index.php and htaccess. Most of what your web application does happens in folders of CodeIgniter. For instance in CI application folder there are subfolders “controllers”, “models” and “views” where you place code for controllers, models and views respectively. CI application folder contains also config files for setting basic features of your web application (URLs, routing, database, cookies etc).

All technical details concerning CodeIgniter are at where you can also download small zipped file with CodeIgniter installation (size is about 2.1MB). There is no CodeIgniter installation process as such – just copy downloaded folders/files in suitable places, edit config files and web application index.php file with the needed information and that is all you need to start creating CodeIgniter web applications.

will be continued