CodeIgniter 3 – Basics, part 1

featured image for CodeIgniter 1 Basics, part 1

CodeIgniter 3 – Basics, part 1

CodeIgniter is web application framework know as MVC (Model View Controller) PHP framework. There are many MVC framework (for instance Laravel, CakePHP, Symfony, Zend Framework, Phalcon, Yii, Kohana and several other) but definitely CodeIgniter enables the quickest start and lets you quickly built small or medium size web application.

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 Laravel, CakePHP, Phalcon, CodeIgniter or something else for one reason: comfort of application development. MVC frameworks 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 and which are developed all the time. This way some pretty much frequent tasks are easier to implement – the code exists there in framework ready to be used, sometimes 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 3 is fact that it is rather well documented and installation process is very simple and very fast. All you need are three folders (two first included in CodeIgniter 3 for installation):

  • application folder
  • system folder
  • folder where your application will be working, accessible via browser

Don’t be confused with names: application folder of CodeIgniter (usually abbreviated to¬†CI) which comes with CodeIgniter and folder where you want to install your web application. Default folder names for all original files of CodeIgniter are: “application” and “system” and they can be renamed if you like. The folder where your application will work (accessible via browser, usually in document root like public_html or htdocs) and 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 (accessible via browser) there are only two files: index.php and .htaccess. All tools your application uses are stored in application and system folders of CodeIgniter. For instance in application folder there are subfolders “controllers“, “models” and “views” where you place code for controllers, models and views respectively. The application folder contains also config files for setting basic features of your web application (URLs, routing, database, cookies etc).

All technical documentation concerning CodeIgniter can be found at where you can also download small zipped file with CodeIgniter installation (size is about 2.6MB for version 3.1.9).

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.

On some web hostings it is impossible to use ssh access and composer, and in such scenario CodeIgniter is the only reasonable PHP MVC framework you can use.