Code Igniter Tutorial

Looking for a complete CodeIgniter tutorial? If so, this article is for you! We’ll show you how to install, configure, and use CodeIgniter to develop a PHP web application.

Also, you will learn about MVC (Model-View-Controller) and its importance in modern web development.

What is CodeIgniter?

is an open source web framework for PHP. It provides many libraries and packages, so you don’t have to create applications and web pages from scratch.

This PHP framework also adds layers of logic to your web applications. Thanks to its MVC (Model-View-Controller) architecture, you can create a cleaner design and parallelize certain processes in the development cycle. We will talk more about this later.

To install CodeIgniter, you need to have SSH access to your . You can use (Windows) or the shell built into the terminal (Linux and macOS).

Without further ado, let’s start this CodeIgniter tutorial.

How to Install CodeIgniter

Installing CodeIgniter requires a .

This application stack is now available on shared hosting. However, if you are using VPS, you may want to learn how to install LAMP on or .

Once everything is ready, you can follow these steps:

  1. Connect to your hosting account via SSH. Next, navigate to the public_html folder by typing: cd /public_html
  2. Download the latest CodeIgniter distribution with this command: wget https://github.com/bcit-ci/CodeIgniter/archive/3.1.11.zip

    In this CodeIgniter tutorial, the latest stable version is 3.1.11. You can visit to see if a newer version has been released.

  3. If you are using shared hosting, you can CodeIgniter by entering the following command: unzip 3.1.11.zip

    Unfortunately, zip might not be pre-installed on the VPS. Therefore, you should install it before running the above command:

    sudo apt-get install zip

  4. You will see a directory called CodeIgniter-3.1.11. We recommend that you rename this folder for convenience: mv /var/www/CodeIgniter-3.1.11 /var/www/codeigniter
  5. Open your browser and enter the path of that folder: https://yourdomain.com/codeigniter

    You should see the default CodeIgniter welcome screen, which means that the framework has been successfully installed on your system.

How to Configure CodeIgniter

In this part of the CodeIgniter tutorial, you will learn how to set up the framework on shared hosting and VPS. For users of the latter, we will also explain how to create a virtual host for this PHP framework.

The first thing is to create a new MySQL database:

  1. In the hPanel, go to the Databases section and select MySQL Databases. Enter the database name and username, and then click Create.
  2. Login to your hosting account via SSH and open the database.php file in the CodeIgniter distribution: nano /codeigniter/application/config/database.php
  3. Find the following section of the file and replace the details with the database information you just created (username, password, database). The rest of the values ​​should be similar to this example: $db = ‘mysql..com’; $db = ‘u499474900_user’; $db = ‘password’; $db = ‘u499474900_database’; $db = ‘mysql’; $db = ”; $db = TRUE; $db = TRUE; $db = FALSE; $db = ”; $db = ‘utf8’; $db = ‘utf8_general_ci’; $db = ”; $db = TRUE; $db = FALSE;

    Save the file by pressing CTRL + X and enter Y to confirm the changes.

  4. Configure your domain name by modifying CodeIgniter’s config.php file. Just enter this command: nano codeigniter/application/config.php

  5. Find the following line: $config = ‘http://yourdomain.com’;

    Change the value to your actual domain name. Next, save the file by pressing CTRL + X and then Y.

You have now successfully synced your database with CodeIgniter!

Configuring Virtual Hosts on VPS

Skip this part if you are installing CodeIgniter on shared hosting.

VPS users have the option to run multiple CodeIgniter applications on a single domain name. To do this, we need to set up virtual hosts and change CodeIgniter settings.

  1. Make sure the document root is in sync with the CodeIgniter installation directory. You can do this by opening the virtual host file: sudo nano /etc/apache2/sites-enabled/000-default
  2. Look for the following block: DocumentRoot /path/to/codeigniter
  3. Change /path/to/codeigniter to the actual path of your CodeIgniter installation directory. By doing this, all your URLs will point to this folder. Save the changes by pressing CTRL + X and Y.

Congratulations, you have successfully created a new virtual host! Now, for a more efficient development process, we need to get rid of the index.php in the CodeIgniter URL format.

  1. Open the config.php file: nano codeigniter/application/config/config.php

    Replace the following line:

    $config = ‘index.php’;

    With this:

    $config = ”;

  2. To make sure you don’t get any errors, you have to redirect future requests by modifying the . But first, you have to check if mod_rewrite is enabled: apache2ctl -M

    The above command will display the capabilities of the server. If you find mod_rewrite listed, continue to the next step. If not, run this line:

    a2enmod rewrite

    Then restart Apache:

    sudo service apache2 restart

  3. Create a .htaccess file in the root folder of CodeIgniter: nano codeigniter/.htaccess
  4. Paste the following lines into the file. Save and exit. RewriteEngine on RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d RewriteRule .* index.php?/$0
  5. Let Apache know to look for the newly created .htaccess file. To achieve this, open the virtual host file again: sudo nano /etc/apache2/sites-enabled/000-default

    Make sure AllowOverride is set to All, as shown below:

    Options Indexes FollowSymLinks MultiViews AllowOverride All Order allow,deny allow from all

    Once confirmed, save the file.

If done correctly, you won’t see the annoying index.php again and your URL will look cleaner.

Understanding MVC

Before continuing with our CodeIgniter tutorial and learning how to develop an application with the framework, you should have a basic understanding of MVC and its concepts.

In a nutshell, MVC is a web development architecture paradigm. It recommends that business logic in any application be separated from presentation.

MVC divides an application into three functional parts:

  • Models — takes care of your database, performs calculations and more. In short, it is where your business logic is located.
  • Views — form the presentation layer of the app, where data from your models is embedded.
  • Controllers — are used to connect models and views. A controller will direct user requests to the appropriate model. After that, once the model has finished its work, the controller will load the relevant view.

This architectural pattern also gives developers the flexibility to reuse code for multiple views. For example, you can apply the same navigation bar to all web pages in your app.

Also, because views and models are completely separate, front-end developers can work in parallel with the back-end team to speed up the development process.

Note that CodeIgniter also adheres to Object Oriented Programming (OOP). As such, models and controllers are PHP classes that extend the base classes provided by the framework.

Views are also named as PHP files, but most of their content is HTML/CSS. There are only a few PHP code snippets, which are used to display the data from the models.

Routing Basics

This is how CodeIgniter formats your strings in the URL:

/index.php////>/

In the previous section, we showed you how to remove the index.php file from the format. Therefore, your URL should look similar to this example:

http://www.yourdomain.com/welcome/tester/1

Here, welcome is a controller class that corresponds to a controller file called welcome.php. This class will call the tester() function, passing ‘1’ as a parameter. More parameters can be provided in a similar way, separated by slashes.

As you can see, routing is effortless in CodeIgniter. You can experiment and create more complex routing.

bookstores

CodeIgniter encourages you to reuse existing libraries and helper functions to perform common tasks.

The framework also allows you to decide which library to load and when to load it. This on-demand process results in fast, lightweight, and feature-rich applications.

Loading a library is easy in CodeIgniter. For example, to load the library from the database, simply pass the following line in your model or controller:

$this->load->database();

However, we suggest that you automatically load the common libraries and helpers when you start the application, so that they are ready when you need them. Examples include and .

Follow these steps to automatically load libraries and helpers:

  1. Open the autoload file: nano application/config/autoload.php
  2. Change the value of autoload:
    • To automatically load the library from the database, replace the following line: $autoload = array();

      With this:

      $autoload = array(‘database’);

    • To automatically load URL helpers, replace the following line: $autoload = array();

      With this:

      $autoload = array(‘url’);

      You can automatically load additional libraries using the array() method and separate them with commas.

  3. Press CTRL + X to save the file and Y to commit the changes.

Create a Simple Application with CodeIgniter

At this point in our CodeIgniter tutorial, you should have a CodeIgniter distribution up and running. You should also know how models, views, and controllers work. Now let’s use the information to create a simple CodeIgniter web application.

The application is quite simple. We will use our model to fetch movie review scores from our database and display them one by one using our view. The controller will be used to direct requests between the model and the view.

Once you’ve mastered the fundamental process, you can start building more complete web applications.

Step 1: Creating a table with phpMyAdmin

Here are the steps to create a table with phpMyAdmin. This table is essential because it forms the basis of our application.

  1. Log in to your account. Navigate to the Databases section and select phpMyAdmin.
  2. Find the database you have synced with CodeIgniter and click Enter phpMyAdmin.
  3. In the Create Table menu, enter the name of the table and the number of columns….
See also  Security in web applications: What is it, how does it work and the best services
Loading Facebook Comments ...
Loading Disqus Comments ...