September 23, 2019

I’ve Installed CodeIgnitor, Now What? 1:4

CodeIgnitor Configuration Guide

You’ve got CodeIgniter installed, but now what? Since you have a new copy of CodeIgniter, before anything else you need to configure it, the setup is generally only one-time thing.

1. Setup the base URL

Open the application/config/config.php file with a text editor and set your base URL. Summarize that your base URL is set as follows:

If Local

$config['base_url'] = "http://localhost/Your folder/";

If Server

$config['base_url'] = "http://example.com/";

2. Database configuration

If you want to use a database with your application, open system/application/config /database.php and set the following array items to corresponding values. Begin by creating a new MySQL database called “hello.db”.

This code connects your web app to “hello.db” MySQL database on localhost with the “root” username.

If Local

$db['default']['hostname'] = "localhost";
$db['default']['username'] = "root";
$db['default']['password'] = "";
$db['default']['database'] = "hello.db";
$db['default']['dbdriver'] = "mysql";

If Server

$db['default']['hostname'] = "localhost"; 
$db['default']['username'] = "Your Username"; 
$db['default']['password'] = "Your Password"; 
$db['default']['database'] = "hello.db"; 
$db['default']['dbdriver'] = "mysql";

Open the file system/application/config/autoload.php and add ‘ database ‘ to the library array of autoloads.

$autoload['libraries'] = array('database');

3. Create and Configure Controller File

The default controller called “welcome.php” is currently in your app setup; this can be found in the system/application/controller folder.

Create a new controller file (home.php) under the directory application/controller/home.php and paste the following code inside:

<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
 
class Home extends CI_Controller {
  public function index()
   {
   $this->load->view('home');
   }
 }
?>

Open the file system/application/config/routes.phpChange the default array to point to the homecontroller.

$route['default_controller'] = "Home"

Create a view home.php under the directory /application/views/home.php. And paste the following code in it.

<!DOCTYPE html>
 <head>
  <meta charset="utf-8">
  <title>My First Website Using Codeigniter</title>
 </head>
  <body>
    <h1>Future Home from Something Big</h1>
  </body>
</html>

5. Create a .htaccess File

There are a few things you can do to enhance your experience with CodeIgniter-such as removing the irritating index.php bit from the URL. By generating a .htaccess file in the root directory and adding the following code, you can achieve this job.

You’ll also need to Open the config.php file (system/application/config) and edit the index page array item to a blank string.

And replace the following line:

$config['index_page'] = 'index.php';

With this:

$config['index_page'] = '';

Once again, Congratulations! We’re going to dive into CodeIgniter much more in detail later. 

And if you have any questions, just leave remark below and ask it away.

Naffys Mir

I'm Naffys Mir, Programming Geek and Founder of CodersStuff.com. Coders Stuff is a Blog Packed with Loads of High-quality Programming Tutorials and a Ton of Engaging Advice and Insight into Front and Back End Technologies.

View all posts

Add comment

Your email address will not be published. Required fields are marked *