Welcome Guest, Not a member yet? Register   Sign In
Need to call a function from another controller
#1

[eluser]Unknown[/eluser]
Hi all

I would like to know how can we call a function from another controller.

e.g.

first controller main.php

class Main extends CI_Controller {


function xyz()
{
echo 'hii';
}
}

second controller sec_main.php

class Sec_main extends CI_Controller {

function abc()
{

**//here i would like to call xyz

}
}

**
I did like
$obj=new main;
$obj->xyz;

but it is giving fatal error.


please help me out.

Thanks in advance
#2

[eluser]toopay[/eluser]
use code tags when you posting your code
Code:
// Remove any space
[code ]<!--YourCodeHere-->[/code ]
#3

[eluser]wiredesignz[/eluser]
[quote author="mayanksingh" date="1304172829"]Hi all

I would like to know how can we call a function from another controller.

...

Thanks in advance[/quote]

Use Modular Extensions - HMVC
#4

[eluser]sofwan[/eluser]
Create your own helper.

So, for your example is like this :

1. Create your helper file in application/helpers
name :test_helper.php
Code:
<?php
      echo "hii";
   ?>

2. In your controller, call your helper :

Code:
<?php
class Sec_main extends CI_Controller {

    function abc()
     {
       $this->load->helper("test_helper");
     }        
  }
?>
#5

[eluser]CroNiX[/eluser]
libraries work well for this too
#6

[eluser]sofwan[/eluser]
Helper and libraries also Model especially for database query can work well. In procedural programming, we can create a function/procedural and call it in main program. In OOP we can create a method and call it in a class/other method but in MVC, there is different way to call other function.




Theme © iAndrew 2016 - Forum software by © MyBB