CodeIgniter Forums
Sharing data accross controllers - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forumdisplay.php?fid=23)
+--- Thread: Sharing data accross controllers (/showthread.php?tid=33823)



Sharing data accross controllers - El Forum - 09-09-2010

[eluser]xare[/eluser]
Hello I have doubt.

Lets imagine I have a couple of controller classes. One called home and the other called contact.

There is a number of data that I want to pass to both controllers' views. For example the title of the page.

The way I know how to do it is by doing the following

file home.php

Code:
<?php

class Home extends Controller {
    
    function Home(){
        parent::Controller();
    }
    function index(){
$data['title']="Title"
$data['content']="Home Page";
$this->load->view('home',$data);
}
}
?>

Now I rewrite code for the contact controller

Code:
<?php

class Contact extends Controller {
    
    function Contact(){
        parent::Controller();
    }
    function index(){
$data['title']="Title"
$data['content']="Contact Page";
$this->load->view('contact',$data);
}
}
?>

Is there any way I can send the variable title to more than one view from a general controller. I mean when I load variables I want to be accessed by more than one view, where could I write them down so to not to copy the code in every controller?


Sharing data accross controllers - El Forum - 09-09-2010

[eluser]n0xie[/eluser]
Make use of base_controller_classes: http://philsturgeon.co.uk/news/2010/02/CodeIgniter-Base-Classes-Keeping-it-DRY


Sharing data accross controllers - El Forum - 09-09-2010

[eluser]ahmed hamza[/eluser]
try tu use session class in CI it will help u to share data across the entire site