Jonathan
Jonathan

Reputation: 543

Add public variable to all CI_Controller's and anything that extends them?

Hi I'd like to add a public variable ($this->data[]) to all instances of CI_Controllers, that way I can store some base rules for outputting a page (css/js, etc) then have each controller append to this array to add its own requirements (more css/js). I have a core library with custom view functions that take those arrays and inject them into the head tag of the page template.

The options I've thought of;

  1. Edit CI_Controller and add it there... guessing that's a bad idea.

  2. Create a shell controller that extends CI_Controller, add the var to that, then have every other controller extend the shell controller.

  3. Any other clever ways?

I've only been using CI for about a month and I've tried reading through the docs but I can't find any built in ways to do something like this? Has anyone encountered this before and if so how did you solve it?

Thanks!

[edit] Using PHP 5.3.x [/edit]

Upvotes: 0

Views: 309

Answers (3)

Glorious Kale
Glorious Kale

Reputation: 1313

You can create your own a base controller file to inherit basic page load methods from in \application\core\MY_Controller.php as such:

<?php defined('BASEPATH') OR exit('No direct script access allowed');

class MY_Controller extends CI_Controller {

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

    protected function loadPage($path, $data = array())
    {
        $this->load->view('common/head'); // Assuming you will use a folder for page parts
        $this->load->view($path, $data);
        $this->load->view('common/foot');
    }
}

Afterwards in your page controller you can call upon the same methods much more easily:

<?php defined('BASEPATH') OR exit('No direct script access allowed');

class User extends MY_Controller {

    public function index()
    {
        $data = array(); // get data from model

        $this->loadPage('user/view', $data); // Assuming you will use folders for sets of views
    }

    public function edit()
    {
        $data = array(); // get data from model

        $this->loadPage('user/edit', $data);
    }
}

Upvotes: 0

stormdrain
stormdrain

Reputation: 7895

http://ellislab.com/codeigniter/user-guide/general/creating_libraries.html

You can extend CI_Controller to have the functionality you want.

application/core/MY_Controller.php:

<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class MY_Controller extends CI_Controller {


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

    public function my_function()
    {
        return "Cool return from my_function";

    }
}

controllers/welcome.php

<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Welcome extends MY_Controller {

    public function index()
    {
        echo $this->my_function();//echo's "Cool return from my_function"
    }
}

You simply define the functionality you want in MY_Controller. Then in your controllers, use extends MY_Controller instead of extends CI_Controller and you can call the functions anywhere inside those controllers.

Upvotes: 2

Alexander Nenkov
Alexander Nenkov

Reputation: 2910

I think what you want can be easily achieved using traits. Check here: PHP: Traits

More specifically -> Example #11 Defining Properties.

The only limitation is it's PHP 5.4+

Upvotes: 1

Related Questions