How To Install Codeigniter In Localhost – Tutorial 1

In the very first tutorial of our series of Codeigniter for beginners, I will teach you how to install Codeigniter in your local computer and setting required things to start building web applications.

What will we cover in this installation tutorial?

  • Install Codeigniter
  • Explore files or project structure
  • Setting Base Url
  • Loading helpers and libraries
  • Removing index.php from url

Let’s do this!

Install Codeigniter

I assume that you already install xampp on your computer. You can read this post of how to install xampp server if stuck.

First of all, you need to download the latest version of Codeigniter framework from its official website.

After downloading the zip file, you need to extract this into your project folder that one you created in htdocs.

Now you need to start your local server which can be xampp or wamp. To run your brand new web app, type following url http://localhost/your-project-folder-name into your browser and press enter.

Congratulation! You have successfully installed the Codeigniter framework in your system.

Let’s have a look at the project structure and explore relevant files.

Explore files or project structure

In this tutorial, I mainly focus on these files and folders.

  1. Index.php
  2. Application Folder
  3. –Config
  4. –Controllers
  5. –Helpers
  6. –Models
  7. –Views

Root file of Codeigniter Framework Index.php

In this file, you can switch between the production, testing, or development mode of your application.

You must set this environment to development if your application is under construction because it gives you the error reporting.

You can also change directory path from index.php file for example if you want to take your views folder outside the application folder you only need to define that new path in index.php and its done.

Now explore the Application folder.

Config Folder.

Inside the application folder, the second folder is a config here you can define your base url, database settings, routes of your application, and autoload helpers, libraries, form validations, and sessions.

Controllers Folder.

Whenever you need to create a new controller for your project, you must create these files inside this folder.

You can set your projects conditional logics here before rendering data to the users and check before passing data from users to the database.

Helpers Directory.

Sometimes you require the same code in your functions or methods that is not a good idea because of a single byte can enough to up and down your applications speed.

So what can you do to overcome, you can merely create a helper and create functions inside this that are going to use throughout in your application.

When you create a helper, you will need to name it like this example_helper.php

You must include _helper after the regular file name.

Remember, when you create a helper without class, then you no need to write public before function.

Now, after creating a helper, you need to autoload or load this helper to use in the application and call the function from everywhere.

Models Directory For Querying Database

Since Codeigniter works on MVC pattern, you should write your database logics in model files. You can store models files in the model folder of the Codeigniter framework.

Views Directory For Rendering HTML & CSS or Dynamic Data From Database.

Now you want to serve content to the users you need to load a particular view file in controllers methods and pass data in that view files.

Fetch data from database using model pass it to the controller and then pass it to the view to render it for users.

Setting Base Url

Base url is the path of your project. You can define base_url in application/config/config.php file, for example, if your project name is myapp then, your base url will be like this, http://localhost/myapp

Loading helpers and libraries.

If you want to use the real power of the Codeigniter framework, then you should load these predefined functions.

You can load these helpers and lib direct inside your controllers and models or autoload them from autoload.php file.

To autoload these helpers and libraries navigate to application->config->autoload.php file.

To autoload a library for form validation then, you can do it like this in your autoload.php file.

$autoload[‘libraries’] = array(‘form_validation’); 

Or you can direct load libraries in your controller and models like this.

$this->load->library(‘form_validation’);

And if you want to autoload helpers then, you can do it like this. 

$autoload[‘helper’] = array(‘url’, ‘common_helper’);

Or you can directly load libraries in your controller and models like this. 

$this->load->helper(‘common_helper’);

Removing index.php from url

Last but not least point of our first Codeigniter framework tutorial is removing index.php from our url.

So let’s have a look at how you can remove this from your applications url.

The first step is, you must create a .htaccess file inside your root directory and paste these lines of code.

<IfModule mod_rewrite.c>

RewriteEngine On

RewriteCond %{REQUEST_FILENAME} !-f

RewriteCond %{REQUEST_FILENAME} !-d

RewriteRule ^(.*)$ index.php/$1 [L]

</IfModule>

and the second step is to navigate to the application->config->config.php file and find this config array.

$config[‘index_page’] = ‘index.php’; 

Now remove index.php from this config array, and you have enough to stretch.

Conclusion

At the end of this tutorial of how to install codeigniter, if you understand this, please do share it with your friends, and social media connections.

If you have any query regarding Codeigniter, please feel free and contact me or you can connect by comments sections I will try to solve your doubts.

Please stay tuned for the next tutorial of this series of Codeigniter framework for beginners.