Welcome Guest, Not a member yet? Register   Sign In
Going berzerk with MY_Controller
#1

[eluser]Marc Arbour[/eluser]
Hi friends.

I have thrown the gloves down with CI for the 1st time since I've used it. I have briefly tought to do it the old PHP way but said to myself that I should ask you guys for help. I have a clean new CI 2.1 running the latest LAMP architecture.

I am trying to make some mathematical calculations before the controlers executions (for ALL controllers) in MY_Controller and use the variable's content (the result) in the controllers. I have tried every approaches I've found on this forum as well as others (stackoverflow to name just that one). I've copy/pasted/changed/tested code like I had never done before. 8 hours later and more then 4 dozen threads... I admit defeat"

Whatever I do and try, MY_Controller (in application/core) works because I can use the variables in the views... But not in the controllers.

Could someone find it in their heart to copy/paste me some working code? I am interested in using the data produced in the MY_Controller to further process in controllers.

Regards.
#2

[eluser]scottwire[/eluser]
Try this:

Code:
class MY_Controller extends CI_Controller
{

public $myVar;

function __construct()
{
  parent::__construct();  
  $this->myVar = "test";
}
}



class Foo extends MY_controller{
    function __construct()
    {
       parent::__construct();
    }


public function index()
{
  echo $this->myVar;
}
}
#3

[eluser]Marc Arbour[/eluser]
THANKS!

On the spot!

I don't know what I have done wrong because I tought I tried that. Or maybe was it in the __construct()

Maybe I should've asked earlier. I spent too much time pulling my hair on this one.

Thanks ScottWire!
#4

[eluser]InsiteFX[/eluser]
The class name doe's not have to be MY_Controller it can be anything you want!
It just has to be saved as ./application/core/MY_Controller.php
#5

[eluser]Marc Arbour[/eluser]
Hi InsightFX

I had tried to "stay on the path" of the other user's example. But having the opportunity to rename the class will help me.

Thanks

Marc




Theme © iAndrew 2016 - Forum software by © MyBB