How to install and setup Codeigniter

Codeigniter is a lightweight PHP-based framework to develop the web application. It is based on MVC (Model View Controller) pattern.

  • The model handles the data processing – Return data, Insert, Update, or delete records from Database Table.
  • The view displays the final output.
  • The controller manages the Model and view. It handles the users’ inputs.

In this tutorial, I cover basis about Codeigniter.

Contents

  1. Download
  2. Configure
  3. CSS and Script
  4. Model
  5. Controller
  6. View
  7. Remove index.php
  8. Conclusion

1. Download

  • Download the Codeigniter from here.
  • Extract zip file on your server and rename the folder e.g. codeigniter.

  • If you are on localhost then run http://localhost/codeigniter/ to check it is working or not.

You will see the following screen if it is successfully run.

2. Configure

Base URL

Open application/config/config.php and set base_url.

Database connection

Set database connection in application/config/database.php.

Default controller

Open application/config/routes.php and edit default_controller value from "welcome" to a new value.

3. CSS and Script

To add external CSS and Script create a new folder at the root.

  • Create new folder public.
  • Within the public folder creates two new folders – css and script.

  • Manage your CSS and script files within it.

Creating a new public/css/style.css file.

4. Model

Navigate to application/models/.

  • Create a new Main_model.php file.
  • Create a class Main_model and extends CI_Model class.
  • Defining two methods getHomeData() and getAboutusData(). From this method returning data.

Completed Code

5. Controller

Navigate to application/controllers/ directory

  • Creating a single controller Pages.php to manage two views (home_view,aboutus_view).
  • Create new Class Pages and extends CI_controller class.
  • Define index() and aboutus() methods from here load and pass model data to view.
  • For using base_url() in view require defining $this->load->helper('url'); in controller.

Model 

Load

Pass the created Model name in $this->load->model("Main_model"); .

Access

Access model method using $this->Main_model->getHomeData();.

Load view

Pass the view name and data in $this->load->view('aboutus_view',$data);.

Completed Code

6. View

Navigate to application/view/ directory.

Here, I am creating 4 new PHP files –

  • header_view.php
  • footer_view.php
  • home_view.php
  • aboutus_view.php

header_view.php

footer_view.php

home_view.php

  • Loading header_view and footer_view with $this->load->view();.
  • Print the passed model data from the controller to the view. For this directly access the value of the name $content (In the controller – $data['content'] = $this->Main_model->getHomeData();).

aboutus_view.php

  • Loading header_view and footer_view with $this->load->view();.
  • Print the passed model data from the controller to the view. For this directly access the value of the name $content (In the controller – $data['content'] = $this->Main_model->getAboutusData();).

Run the project –

When you click About us menu you will see the following URL.

  • Here, http://localhost/codeigniter_beginner/index.php is site URL.
  • /pages – Is the name of the controller.
  • /abouts – Is the method name of the controller.

If you want to remove index.php from the URL then you need to create .htaccess file at the project root.

7. Remove index.php

Navigate to application/config/config.php and replace $config['index'] = "index.php"; with $config["index"] = "";.

Edit application/views/header_view.php

Here, I removed / before pages from the link.

.htaccess file

  • Create .htaccess file in your root folder.
  • Write following code in it –

Now run.

You don’t see index.php on your URL.

8. Conclusion

If you want to use base_url value which defined in application/config/config.php file then you need to define $this->load->helper('url'); in your controller file.

Share on:

Hello, I am Dharmendra Yadav and I am a Python Developer with experience in web development using Django, Flask, REST API, SQL, MySQL, HTML, CSS, JavaScript, WordPress, Oracle Cloud, AWS and Git. I also write technical articles where I explain web development and Software Engineering. Facebook , Linkedin

Leave a Comment