Codeigniter tutorial

First Page With CodeIgniter Tutorial For Beginners

Views: 635

First Page With CodeIgniter Tutorial

As mentioned in the previous post that codeigniter is a loosely based MVC framework and holds 3 main tiers namely Model View and Controller. In this chapter we will learn how to create the very first page in codeigniter “The Home Page” of your project.

 

Let’s start

Requirements:

A local server installed in your system or a hosting account.

A text editor : Notepad, Notepad++, Sublime text, Net beans, Dreamweaver or any text editor.

 

Start your local server or log in to your hosting provider with FTP Client.

 

First of all let us create a database connection for codeigniter

First of all create a database in MySql, then go to your codeigniter directory, then open Application directory, then inside the config directory you will find a file named database.php, open that file with your preferred text editor. On line number 76 you will find a array variable named $db[‘default’], put in your database hostname username and password in there. Refer the picture below for hint.
database connection codeigniter

Make sure you put in the correct values. By default in local installation the username is root, password is null or blank and hostname is localhost. Save database.php and close it. Now open autoload.php in same directory and set autoload variable to load database like this $autoload[‘libraries’] = array(‘database’);

This will automatically load the database library whenever the app is initiated.

Now open your browser and go to url of your codeigniter installation. If you see a database error then you have misconfigured your database connection details. If you see a healthy welcome page with no error message on it then congratulation you have learned how to make a database connection in codeigniter.

create-a-controller

Now let us create a codeigniter controller:

Now come out of the config directory, move in to the controllers directory, create a new file name it Home.php , make sure you keep the first letter uppercase, in codeigniter the naming of models and controllers start in uppercase. Once you have done creating Home.php open both Welcome.php and Home.php with your text editor.

See how a controller starts in Welcome.php and do same with Home.php copy and paste everything in Welcome.php to Home.php. Check line 4.

class Welcome extends CI_Controller , check the Welcome is with uppercase W, it is because in codeigniter class name of controllers and models start with uppercase, it is mandatory for codeigniter and the important rule to follow. Now inside your Home.php controller rename the class name from Welcome to Home because it is also important that class name should be same as the controller name.

 

Note: Class names of models and controllers start with uppercase in codeigniter.

Creating the first page with codeigniter:

Now we are all set to create anddisplay our first page. Inside the index function of the Home controller echo any text you want to be displayed. Like the example below

public function index()

{

echo ‘My first page with codeigniter’;

}

Browsing your first page with codeigniter:

Now move to your browser, in the addressbar write the url of your codeigniter installation followed by index.php/home i.e. http://localhost/codeigniter/index.php/home. You will see the text is printed on screen, I am adding a screenshot.

Print codeigniter output

If you have done this setup perfectly then we will move to next “working with codeigniter views”.

Comments: 2

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