Welcome Guest, Not a member yet? Register   Sign In
Accessing MY_Controller vars from hook
#1

[eluser]senic[/eluser]
Hi,

i've been trying to make CI work for my but i stumbled accross a little issue.

I extended the Controller to get a few things like login covered and i'm also using a small template system to have some support for partials.

So, i feed the $template var with the always-there stuff and add the dynamic things from the News controller. I could do the actual rendering of the page itself in the News controller of course, but why write the same line in every controller i need over and over again. Wink

So i tried to put the actual rendering in a post_controller hook (Template class below), but the problem is that it seems like the $template var is not accessible there or to be more clear, the dynamic things getting added from the News controller are missing. Only the things added in the constructor of MY_Controller() are there, so it looks like it gets overwritten.

Any idea how to make the MY_Controller variable available in a hook just how they are available in a regular controller?

MY_Controller lib
Code:
class MY_Controller extends Controller  {
    
    var $template = array();
    
    function MY_Controller() {
        parent::Controller();
        $this->loginbox();
        $this->createmenubar();
    }

Controller
Code:
class News extends MY_Controller {
    
    function News() {
        parent::MY_Controller();
    }

    function index()
    {
        [... feeding $data ...]
        $this->template['content'] = $this->load->view('content_news', $data, TRUE);
    }
}

Hook
Code:
class Template extends MY_Controller {
    
    function Template() {
        parent::MY_Controller();
    }

    function index()
    {
        $this->load->view('template', $this->template);
    }
}
#2

[eluser]danmontgomery[/eluser]
Code:
$CI =& get_instance();
$CI->some_library->some_function();
#3

[eluser]senic[/eluser]
Thanks for the answer.

Code:
class Template {
    
    function output()
    {
        $CI =& get_instance();
        $CI->load->view('template', $CI->template);
    }
}

I've been trying this, but for some reason the browser window remains empty after calling load->view form the hook.
#4

[eluser]senic[/eluser]
*bump*

Can anyone help with this?
#5

[eluser]danmontgomery[/eluser]
Have you tried adding output after that line to see if the script is still executing after it, or is it a PHP error? Anything in the CI or apache error logs?
#6

[eluser]senic[/eluser]
For some weird reason it suddenly works now, so case closed.
#7

[eluser]Zack Kitzmiller[/eluser]
I'm happy that it's not working for you, but you should probably figure out why it was broken in the first place. Otherwise you're going to hunting it down later. Possibly after it's production.




Theme © iAndrew 2016 - Forum software by © MyBB