Home » Php » php – How to call codeigniter controller function from view

php – How to call codeigniter controller function from view

Posted by: admin April 23, 2020 Leave a comment


How to call codeigniter controller function from view? When i call the function in a controller, get a 404 page.

How to&Answers:

Codeigniter is an MVC (Model – View – Controller) framework. It’s really not a good idea to call a function from the view. The view should be used just for presentation, and all your logic should be happening before you get to the view in the controllers and models.

A good start for clarifying the best practice is to follow this tutorial:


It’s simple, but it really lays out an excellent how-to.

I hope this helps!


You can call controller function from view in the following way:


public function read() {
    $object['controller'] = $this; 
    $this->load->view('read', $object);


// to call controller function from view, do


You can call a controller function with AJAX on your view.
In this case, I’m using the jQuery library to make the call.

<script type="text/javascript">
            url: "<?=site_url("controller/function")?>",
            type: "post", // To protect sensitive data
            data: {
               variableX: "string",
               variableY: 25
               //and any other variables you want to pass via POST
            // Handle the response object

This way you can create portions of code (modules) and reload them the AJAX method in a HTML container.


I would like to answer this question as this comes all times up in searches —

You can call a controller method in view, but please note that this is not a good practice in any MVC including codeigniter.

Your controller may be like below class —

    class VCI_Controller extends CI_Controller {
    function abc($id){
       return $id ;


Now You can call this function in view files as below --
    $CI =& get_instance();
    $CI->abc($id) ;



class MY_Controller extends CI_Controller {

    public $CI = NULL;

    public function __construct() {
        $this->CI = & get_instance();

    public function yourMethod() {



// in view just call


views cannot call controller functions.


One idea i can give is,

Call that function in controller itself and return value to view file. Like,

class Business extends CI_Controller {
    public function index() {
            $data['css'] = 'profile';

            $data['cur_url'] = $this->getCurrURL(); // the function called and store val
     function getCurrURL() {
            return $currURL;


in view(home_view.php) use that variable. Like,

echo $cur_url;


I know this is bad..
But I have been in hard situation where it is impossible to put this back to controller or model.

My solution is to call a function on model.
It can be do inside a view.
But you have to make sure the model has been loaded to your controller first.

Say your model main_model, you can call function on the model like this on your view :

Hope this help. 🙂


We can also pass controller function as variable in the view page.

class My_controller extends CI_Controller {
    public function index() {
                               return $val*$val;

In the view page

<p>Square of <?=$val?>
     echo $square($val); 

The output is 9


it is quite simple just have the function correctly written in the controller class and use a tag to specify the controller class and method name, or any other neccessary parameter..

if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Iris extends CI_Controller {
    function __construct(){

    }public function pledge_ph(){

This is the controller class Iris.php
and the model class with the function pointed to from the controller class.

if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Script extends CI_Model {
    public function __construct() {
        // Your own constructor code
    }public function ghpledge(){
        $gh_id = uniqid(rand(1,11));
        $gh_member = $_SESSION['member_id'];
        $amount= 10000;
        $data = array(
            'gh_id'=> $gh_id,
            'gh_member'=> $gh_member,
            'amount'=> $amount,
            'date'=> $date

On the view instead of a button just use the anchor link with the controller name and method name.

        <a href="<?php echo base_url(); ?>index.php/iris/pledge_ph" class="btn btn-success">PLEDGE PH</a>


Try this one.

Add this code in Your View file

$CI     = & get_instance();
$result = $CI->FindFurnishName($pera);

Add code in Your controller File

public function FindFurnishName($furnish_filter)


$FindFurnishName        = $this->index_modal->FindFurnishName($furnish_filter);  
$FindFurnishName_val    = '';
foreach($FindFurnishName as $AllRea)
    $FindFurnishName_val  .= ",".$AllRea->name;
return ltrim($FindFurnishName_val,',');



  1. FindFurnishName is name of function which is define in Your Controller.
  2. $pera is a option ( as your need).


if you need to call a controller from a view,
maybe to load a partial view,
you thinking as modular programming,
and you should implement HMVC structure in lieu of plane MVC.
CodeIgniter didnt implement HMVC natively,
but you can use this useful library in order to implement HMVC.

after setup
remember:that all your controllers should extends from MX_Controller in order to using this feature.


Go to the top of your View code and do it like this :

     $MyFunctionReturnValue = $this->MyModelName->MyFunctionName($param));

<div class="row">
    Your HTML CODE