CodeIgniter Configuration

CodeIgniter Configuration Tutorial

This tutorial will show you how to configure CodeIgniter for the rest of the tutorials in our CodeIgniter Tutorials Series.

I will use Bootstrap Framework which is used for developing responsive HTML projects.

Bootstrap will make our project more eye pleasing than plain HTML, unless you want to make this with your own styles.

You will need Bootstrap source if you want to follow these tutorials.

Bootstrap configuration

Create new folder named style in the root of your project, you can name it style

Extract folders: css, fonts and js from dist directory that you downloaded.

I will use an example provided in Bootstrap archive called sticky-footer-navbar which you can find in docs/examples directory or you can create a new .css file in css directory and insert following code

/* Sticky footer styles
-------------------------------------------------- */
html {
  position: relative;
  min-height: 100%;
}
body {
  /* Margin bottom by footer height */
  margin-bottom: 60px;
}
.footer {
  position: absolute;
  bottom: 0;
  width: 100%;
  /* Set the fixed height of the footer here */
  height: 60px;
  background-color: #f5f5f5;
}


/* Custom page CSS
-------------------------------------------------- */
/* Not required for template or sticky footer method. */

body > .container {
  padding: 60px 15px 0;
}
.container .text-muted {
  margin: 20px 0;
}

.footer > .container {
  padding-right: 15px;
  padding-left: 15px;
}

code {
  font-size: 80%;
}

Now, your project structure should look like this:

├── application
├── composer.json
├── contributing.md
├── index.php
├── license.txt
├── readme.rst
├── style
│   ├── css
│   │   ├── bootstrap.css
│   │   ├── bootstrap.css.map
│   │   ├── bootstrap.min.css
│   │   ├── bootstrap.min.css.map
│   │   ├── bootstrap-theme.css
│   │   ├── bootstrap-theme.css.map
│   │   ├── bootstrap-theme.min.css
│   │   ├── bootstrap-theme.min.css.map
│   │   └── sticky-footer-navbar.css
│   ├── fonts
│   │   ├── glyphicons-halflings-regular.eot
│   │   ├── glyphicons-halflings-regular.svg
│   │   ├── glyphicons-halflings-regular.ttf
│   │   ├── glyphicons-halflings-regular.woff
│   │   └── glyphicons-halflings-regular.woff2
│   └── js
│       ├── bootstrap.js
│       ├── bootstrap.min.js
│       └── npm.js
└── system

Database Configuration

In tutorial with setting up CodeIgniter I assumed that you already have a database and we entered database credentials.

Now, we need new table in database for data that we create.

Lets say we want to create hardware components inventory, so I’ll create new table named components.

It will have three columns:

ComponentID – Integer – Primary key for this table

ComponentName – Varchar – Name for the component

ComponentNo – Integer – Number of components available

You can use phpMyAdmin for creating database or you can import code bellow to database which has table components

SET SQL_MODE = "NO_AUTO_VALUE_ON_ZERO";
SET time_zone = "+00:00";

CREATE TABLE IF NOT EXISTS `components` (
`ComponentID` int(11) NOT NULL,
  `ComponentName` varchar(64) COLLATE utf8_unicode_ci NOT NULL,
  `ComponentNo` int(11) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;

ALTER TABLE `components`
 ADD PRIMARY KEY (`ComponentID`);

ALTER TABLE `components`
MODIFY `ComponentID` int(11) NOT NULL AUTO_INCREMENT;

Now, when we have our database ready, we can proceed with CodeIgniter configuration.

CodeIgniter Configuration

Open config.php file in application/config folder

Find line

$config['base_url'] = '';

and enter your website address

$config['base_url'] = 'http://your_website/codeigniter/';

When you load some controller with base_url configured it will look like this

http://your_website/codeigniter/index.php/controller_name

If you want to remove index.php from URL, you need to remove index.php from index_page option in config.php, so it will look like this:

$config['index_page'] = '';

And for this to work on your server you need to have mod_rewrite enabled.

You can close config.php file for now.

Go to root folder of CodeIgniter and create file .htaccess

NOTE: Make sure you enter . (dot) in front of htaccess

Copy this code to .htaccess file

<IfModule mod_rewrite.c>
  RewriteEngine On
  RewriteBase /codeigniter/
  RewriteCond %{REQUEST_FILENAME} !-f
  RewriteCond %{REQUEST_FILENAME} !-d
  RewriteRule ^(.*)$ index.php?/$1 [L]
</IfModule>

<IfModule !mod_rewrite.c>
  # If we don't have mod_rewrite installed, all 404's
  # can be sent to index.php, and everything works as normal.

  ErrorDocument 404 /index.php
</IfModule>

Make sure that you edit RewriteBase line to match your CodeIgniter folder.

So, if CodeIgniter is located at

http://your_website/codeigniter

Your RewriteBase line should look like this

RewriteBase /codeigniter/

You should now be able to access CodeIgniter’s controller without index.php in URL

http://your_website/codeigniter/controller_name

CodeIgniter is now configured and ready for our next tutorial.

Leave a Reply

Your email address will not be published.