Welcome Guest, Not a member yet? Register   Sign In
Were to store common view data
#1

[eluser]mptre[/eluser]
Hello!
I have a question regarding "best practices" when it comes to Codeigniter. I have several controllers which uses the same views. In these views there's always some mandatory data such as the links for the navigation. I need to access this data in all my controllers. So therefor my question is where should the data be stored? Storing it inside each controller doesn't feel that efficient since I need to repeat myself. Is using a model for this considered bad practices?

Thanks in advantage!
#2

[eluser]Rob Gordijn[/eluser]
look at the following thread:

http://ellislab.com/forums/viewthread/143648/
#3

[eluser]mptre[/eluser]
Thanks!
But I'm having problem getting this to work. Can't spot anything wrong with the code below. I get an error saying that data is a undefined property. Any ideas on what I'm doing wrong here?

controllers/foo.php
Code:
<?php
class Foo extends Controller {

    function Foo() {
        parent::Controller();
    }

    function index() {
        die(var_dump($this->data));
    }

}
?>

libraries/MY_Controller.php
Code:
<?php
class MY_Controller extends Controller {

    public $data;

    function MY_Controller() {
        parent::Controller();

        $this->data = array();
    }

}
?>
#4

[eluser]Rob Gordijn[/eluser]
hey

you need to extend your Foo class with the MY_Controller Smile
#5

[eluser]momo123[/eluser]
I think it should be:

class Foo extends !!!MY_Controller!!!

Good luck!
momo
#6

[eluser]mptre[/eluser]
Correct my if I'm wrong. But in that case I have to write an extension foreach controller?
#7

[eluser]Rob Gordijn[/eluser]
you create one MY_Controller, and then extend all you controllers with that one Smile
#8

[eluser]Michael Wales[/eluser]
Are the links for navigation actual dynamic data in which you must query a database or is it just static xHTML. If it's just static $this->load->view() can be called from within a view itself.
#9

[eluser]mptre[/eluser]
Thanks guys! Works just fine now.




Theme © iAndrew 2016 - Forum software by © MyBB