Controller
What is Controller?

First time when we run project, then we will see by default welcome controller run.

You can see in controllers folder there are welcome.php file this is a controller, you open then and see your class name Welcome

Here you can name of file and class must be same, but class name must be start with capital letter. 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 by default index function is call, Now you want to create your own function like this.

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 Then you controller name then your function name.

Exact Path like

http://127.0.0.1/classified/index.php/welcome/test

where welcome is your controller name and test is function

How to create your own controller

Suppose you want to create Mytest controller.Now create page name as Mytest.php

<?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 and controller should be startwith capital letter and filename must be start with small letter.

How to Run Controller

Same as above


open your project then index.php then controller name

like this

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


Previous
Next