Introduction to PHP based CodeIgniter Framework

<h2>1. What is CodeIgniter?</h2>

CodeIgniter (CI) is a free PHP based MVC framework with tons of libraries. The documentation is easy enough for a beginner to build a website. Since CI strictly follows the MVC, it is easy to separate views (templates) and business and presentation logics and thus it is convenient to work on a project with Front End and Back End programmers. CI is Faster than other frameworks (because its library consumes less memory), SEO friendly URLs (without php extension), and provides easy documentation for beginners.

Some of the important features of CodeIgniter are listed below:
- Model-View-Controller (MVC) Based System
- Extremely Light Weight
- Full-Featured database classes with support for several platforms.
- Query Builder Database Support
- Form and Data Validation
- Security and XSS Filtering
- Session Management
- Email Sending Class. Supports Attachments, HTML/Text email, multiple protocols (sendmail, SMTP, and Mail) and more.
- Image Manipulation Library (cropping, resizing, rotating, etc.). Supports GD, ImageMagick, and NetPBM
- File Uploading Class
- FTP Class
- Localization
- Pagination
- Data Encryption
- Benchmarking
- Full Page Caching
- Error Logging
- Calendaring Class
- User Agent Class
- Zip Encoding Class
- Template Engine Class
- Trackback Class
- XML-RPC Library
- Unit Testing Class
- Search-engine Friendly URLs
- Flexible URI Routing
- Support for Hooks and Class Extensions
- Large library of "helper" functions

I would like to introduce CodeIgniter framework in following sections:

##<h2>2. Installation Instructions</h2>
It is very easy to install CodeIgniter. Just follow the steps given below:
###**Step-1:** Download the CodeIgniter (latest version: CodeIgniter v3.1.11) from the link http://www.codeigniter.com/download
###**Step-2:** Unzip the folder and rename it to your desired project name, for our example, we renamed it as "codeigniter-test"
###**Step-3:** Upload all files and folders to your server 
(for windows xampp server copy the "codeigniter-test" folder to /xampp/htdocs/)
###**Step-4:** After uploading all the files to your server, visit the URL of your server, e.g., www.domain-name.com.
(for xampp local server, we can access our site by https://codeigniter-test.localhost/)

The welcome page is as follows
![welcome page.PNG](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/488641/fd3d3792-1922-cc50-8912-9f440cfce117.png)

##<h2>3. CodeIgniter Application Architechture</h2>

![Application+Architecture.jpg](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/488641/b503accb-07d0-d921-3980-cb5f9c43a719.jpeg)

- As shown in the figure, whenever a request comes to CodeIgniter, it will first go to **index.php** page.

- In the second step, **Routing** will decide whether to pass this request to step-3 for caching or to pass this request to step-4 for security check.

- If the requested page is already in **Caching**, then **Routing** will pass the request to step-3 and the response will go back to the user.

- If the requested page does not exist in **Caching**, then **Routing** will pass the requested page to step-4 for **Security** checks.

- Before passing the request to **Application Controller**, the **Security** of the submitted data is checked. After the Security check, the **Application Controller** loads necessary **Models**, **Libraries**, **Helpers**, **Plugins** and **Scripts** and pass it on to **View**.

- The **View** will render the page with available data and pass it on for **Caching**. As the requested page was not cached before so this time it will be cached in **Caching**, to process this page quickly for future requests.

##<h2>4. Directory Structure of CodeIgniter</h2>
![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/488641/70037e14-dba9-6f5d-e2fa-989f56482346.png)

The CodeIgniter directory structure is divided into 3 folders: 
📁　Application
📁　System
📁　User_guide

###<h3>📁Application</h3>
As the name indicates the Application folder contains all the code of your application that you are building. This is the folder where you will develop your project. The Application folder contains several other folders, which are explained below −

**Cache** − This folder contains all the cached pages of your application. These cached pages will increase the overall speed of accessing the pages.
**Config** − This folder contains various files to configure the application. With the help of **config.php** file, user can configure the application. Using **database.php** file, user can configure the database of the application.
**Controllers** − This folder holds the controllers of your application. It is the basic part of your application.
**Core** − This folder will contain base class of your application.
**Helpers** − In this folder, you can put helper class of your application.
**Hooks** − The files in this folder provide a means to tap into and modify the inner workings of the framework without hacking the core files.
**Language** − This folder contains language related files.
**Libraries** − This folder contains files of the libraries developed for your application.
**Logs** − This folder contains files related to the log of the system.
**Models** − The database login will be placed in this folder.
**Third_party** − In this folder, you can place any plugins, which will be used for your application.
**Views** − Application’s HTML files will be placed in this folder.

###<h3>📁System</h3>
This folder contains CodeIgniter core codes, libraries, helpers and other files, which help make the coding easy. These libraries and helpers are loaded and used in web app development.

This folder contains all the CodeIgniter code of consequence, organized into various folders −
**Core** − This folder contains CodeIgniter’s core class. Do not modify anything here. All of your work will take place in the application folder. Even if your intent is to extend the CodeIgniter core, you have to do it with hooks, and hooks live in the application folder.
**Database** − The database folder contains core database drivers and other database utilities.
**Fonts** − The fonts folder contains font-related information and utilities.
**Helpers** − The helpers folder contains standard CodeIgniter helpers (such as date, cookie, and URL helpers).
**Language** − The language folder contains language files. You can ignore it for now.
**Libraries** − The libraries folder contains standard CodeIgniter libraries (to help you with e-mail, calendars, file uploads, and more). You can create your own libraries or extend (and even replace) standard ones, but those will be saved in the **application/libraries** directory to keep them separate from the standard CodeIgniter libraries saved in this particular folder.

###<h3>📁User_guide</h3>
This is your user guide to CodeIgniter. It is basically, the offline version of user guide on the CodeIgniter website. Using this, one can learn the functions of various libraries, helpers and classes. It is recommended to go through this user guide before building your first web app in CodeIgniter.

Besides these three folders, there is one more important file named **index.php**. In this file, we can set the application environment and error level and we can define system and application folder name. It is recommended, not to edit these settings if you do not have enough knowledge about what you are going to do.

##<h2>5. Create a new view page</h2>
The default controller is Welcome controller which is located at **application\controllers\Welcome.php**. 

###<h3>Step 1: Create a Controller</h3>
Now, for a test we will create another Controller named Sample **(application\controllers\Sample.php)** as follows:

```php5:PHP
<?php
class Sample extends CI_Controller {

  public function index(){
    echo "Hello World!";
  }

  public function hello(){
    echo "This is hello function.";
  }

  public function test(){
    $this->load->view('test');
  }

}
?>

```
###<h3>Step 2: Create a view page (template)</h3>
Now, we will create a view page named Test.php (application\views\test.php) that the Sample controller will call like following:

```HTML:test.php
<!DOCTYPE html>
<html lang="en">

<head>
 <meta charset="utf-8">
 <title>CodeIgniter View Example</title>
</head>

<body>
 CodeIgniter View Example
</body>

</html>

```

###<h3>Step 3: Access page by hitting siteurl/index.php/controller/method/ </h3>

For the above example, Controller name is **Sample**, the method name is *test*, so, our URL will be like, https://codeigniter-test.localhost/index.php/sample/test

So, If we hit the **https://codeigniter-test.localhost/index.php/sample/test** then we will see the following page

![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/488641/a22643c1-f6be-bfdd-d2a8-7ed8df3f06d5.png)

###<h3>Step 4: We can clean the URL by following methods, that is we can remove index.php from the URL. </h3>

For removing index.php from the URL **https://codeigniter-test.localhost/index.php/sample/test** →**https://codeigniter-test.localhost/sample/test**
####(a). Replace the following config setting from **application\config\config.php**
`$config['index_page'] = 'index.php';`　→　`$config['index_page'] = '';`　and 
make sure the `$config['uri_protocol'] = 'REQUEST_URI';` or `$config['uri_protocol'] = 'PATH_INFO';`

```php5:config.php
<?php

$config['index_page'] = '';

$config['uri_protocol']	= 'REQUEST_URI';
?>

```

####(b). Place the .htaccess in the root folder of your project (codeigniter-test)

```html:.htaccess
<IfModule mod_rewrite.c>
  RewriteEngine On
  RewriteBase /
  RewriteRule ^index\.php$ - [L]
  RewriteCond %{REQUEST_FILENAME} !-f
  RewriteCond %{REQUEST_FILENAME} !-d
  RewriteRule . /index.php [L]
</IfModule>

```

Now, if we hit by https://codeigniter-test.localhost/sample/ that contains only the controller name, meaning to load the index method of Sample Controller. Since we put `echo "Hello World!";` in the index method, the following page will be displayed:
![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/488641/35d3b260-9a07-c8f1-72a2-42d06cc22fc3.png)
If we hit by https://codeigniter-test.localhost/sample/test the following screen will appear
![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/488641/ffe5488f-9b78-8f8f-8f17-31aa216dceec.png)

####(c). Add a suffix in URLs
If we want a add suffix **.html** in URLs in CodeIgniter its very easy.

Replace the following config setting from **application\config\config.php**
`$config['url_suffix'] = '';`　→　`$config['url_suffix'] = '.html';`

```php5:config.php
<?php

$config['url_suffix'] = '.html';

?>

```

Now, we can access pages by .html suffix, that is, we can access the test page by https://codeigniter-test.localhost/sample/test.html and https://codeigniter-test.localhost/sample/test.
![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/488641/ba4a5a75-7f51-1ff2-ce3b-568d5c572fcd.png)

---