Help! How to set some general (global) vaiables? |
[eluser]victorche[/eluser]
It's been a while, since I asked almost the same question and I still can not find the solution. For example, I have several controllers like "blog", "search", "faq". But I have header everywhere (in every single page). And in this header, I have a navigation menu, with links, generated from a db result. The header view looks like this: Code: <div id="menu"> So in my MY_Controller.php I am loading this general model like this: Code: $this->load->model('general_model', 'general'); Code: $links = $this->general->get_links(); Can I avoid this duplication of code? Imagine the blog controller... I have there function index (for the main blog view), function categories (to display posts from custom category), function view (to view a single post with the comments). And everytime I have to use $links = $this->general ... and then in the $data array ... 'top_menu' => ... I tried at least to deal with the $links variable. I put this line in MY_Controller.php like: Code: class MY_Controller extends Controller {
[eluser]gyo[/eluser]
This issue has been discussed a lot in the forum, next time search before. I guess you need to load a view in a view: Code: $data['header'] = $this->load->view('header', '', true); This way the 'main_template' will be sent to the browser, and the variable '$header' inside it will be replaced with the 'header' view. Note that the 'header' has been loaded as data with the 'true' paramenter, so that it's ready to be embedded in another view. FYI: http://ellislab.com/codeigniter/user-gui...views.html
[eluser]victorche[/eluser]
Thanks a lot, but this is not my problem. I am using Phil's template library and in my case I am asking for a variable, which I want to set in MY_Controller.php. And believe me, I was searching the forum for 2 weeks already. That's why I am creating something like a general (common) model, which functions can be used globally, on every page. And I am loading this model (general_model.php) in MY_Controller.php I am not sure if this is the right approach, but this way I can use all the common functions from general_model.php all over the site (in different controllers). But I want something else. I want to declare a variable in one file and to use it in different controllers. Just because, as in this example with the menu links, I have these links on every page. So instead of using this in every controller: Code: $links = $this->general->get_links(); But it is not working. The general model is loaded, but declaring the variable $links there is not enough.
[eluser]gyo[/eluser]
Alright, sorry I didn't get your problem. ![]() Just a quick thought... what about this? Code: $this->links = $this->general->get_links(); When you set $this->links inside MY_Controller, it will be available for all the controllers that extend it, the same way: $this->links. Hope it helps!
[eluser]victorche[/eluser]
[quote author="gyo / suashi" date="1281364019"]Alright, sorry I didn't get your problem. ![]() Just a quick thought... what about this? Code: $this->links = $this->general->get_links(); When you set $this->links inside MY_Controller, it will be available for all the controllers that extend it, the same way: $this->links. Hope it helps![/quote] Great ... but how should I call them in controllers? Code: // Like this:
[eluser]gyo[/eluser]
Like this: Code: $data = array( You might need to set the variable in MY_Controller as public if you're using PHP5.
[eluser]victorche[/eluser]
[quote author="gyo / suashi" date="1281367143"]Like this: ... You might need to set the variable in MY_Controller as public if you're using PHP5.[/quote] Really thanks :] But another reply means another question :] How can I set the variable as public? I know I can set a function as public, but variable... Code: public function this_is_public()
[eluser]John_Betong[/eluser]
I use numerous Controllers with a common MY_Controller. Each individual controller only loads specific view $data and then the $data is passed through to a lengthy common method MY_Controller->view($data). This common view method then tests and loads other information before a final call to a single common view.
[eluser]John_Betong[/eluser]
duplicate of my previous post which I am not authorised to delete. Quote:Really thanks :] Set your public variable in MY_Controller Code: class MY_Jokes extends Controller
[eluser]victorche[/eluser]
@John_Betong, thanks ... It means I can do something like: Code: public $this->links = $this->general->get_links(); |
Welcome Guest, Not a member yet? Register Sign In |