Controller
What is Controller?

First time when we run project, then we will see by default welcome controller run and show welcome_message page

To View Controller Goto:

application->controllers->Welcome

Here you can see file name and class name both are same name with first letter capital that is Welcome, Here you can also see Welcome class inherit CI_Controller.

Welcome Controller code

if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Welcome extends CI_Controller {

public function index() {

$this->load->view('welcome_message');

}

}

In this code we see by default index function is create, Now we create own function inside this class.

How to Create a Function in Controller

Now You can see in past by default index() function is created, But I need More function in this controller suppose aboutus page , contactus page etc

<?php
defined('BASEPATH') OR exit('No direct script access allowed');

class Welcome extends CI_Controller {

public function index()
{
$this->load->view('welcome_message');
}

public function aboutus()
{
echo 'This is aboutus function';
}
}



How to Run this

First Open your project then type index.php/welcome/aboutus

Here :

welcome is controller name

aboutus is function

How to create your own controller

Suppose you want to create User controller Then create file User.php and save in application->controllers

<?php
defined('BASEPATH') OR exit('No direct script access allowed');

class User extends CI_Controller {

public function index()
{
echo "This is my new controller";
}

}

Note: Here class name and pagename should be same start with capital letter

How to Run Controller

http://127.0.0.1/classified/index.php/user

here user is controller name. if we run url then this will call index because we have not write any function name.


Previous
Next