cam
cam

Reputation: 23

PHP CI - Passing a variable from MY_controller to a method

I'm trying to declare the title variable in MY_Controller in the $data variable, to use it when I load the page so I do that:

function __construct()
    {
     parent::__construct();

    $this->data['siteNamePrefix'] = "Mysite.com - ";

     }

So when I loa my page in the method of a normale controller,

class Home extends MY_Controller {

function __construct()
{
    parent::__construct();
}

public function index()
{
    var_dump($data);
    $this->load->view('home',$this->data);
}

I don't see in the var_dump the variable siteNamePrefix that I declared in MY_controller

Upvotes: 1

Views: 683

Answers (1)

Michael Berkowski
Michael Berkowski

Reputation: 270637

Assuming the __construct() at the top of your post is MY_Controller::__construct(), you have correctly extended it.

In the object instance, you just need to be using $this in the dump as well as in the load() method that follows it:

public function index()
{
    // $this->data should be defined here and correctly initialized
    var_dump($this->data);
    $this->load->view('home',$this->data);
}

Upvotes: 1

Related Questions