Welcome Guest, Not a member yet? Register   Sign In
Construct & destruct - quick question
#1

[eluser]hightower[/eluser]
I'm just starting with CI, and it is my first framework so I'm still getting new to it. I have a quick question though. Does it provide a destruct function similar to that in procedural PHP?

Code:
function __construct()
{
}

function __destruct()
{
}

The reason I ask is because I would want to do something like this:

Code:
class Welcome extends Controller()
{

    function Welcome()
    {
        $this->load->view('header');
    }

    function index()
    {
        $this->load->view('content');
    }

    function something_else()
    {
        $this->load->view('something_else');
    }

    function __destruct()
    {
        $this->load->view('footer');
    }

}

I've tried using the __destruct function shown in the above example, but I believe this is carried out after the view has been made so therefore doesn't work. Is there anything that would achieve this so it becomes part of the view?

It's not a priority, just a little thing that would be nice to save me loading the footer everytime.

Thanks Smile
#2

[eluser]TheFuzzy0ne[/eluser]
Loading a view in the desctructor is probably not the best place to do this. There are lots of topics on the forum pertaining to this problem. The solution is to create a main template file, which loads the header, navigation, footer and anything else, and also loads the content dynamically depending on the variable you pass to it.

Main template
Code:
<?php $this->load->view('header'); ?>
<?php $this->load->view('navigation'); ?>
<?php echo $content; ?>
<?php $this->load->view('footer'); ?>

Controller Method
Code:
function index()
{
    $this->load->vars(
        array
        (
            'content' => $this->load->view('some_view', null, TRUE),
            'another_var' => 'some_value',
        )
    );

    $this->load->view('main_template');
}

I hope this helps.

EDIT: Fixed a few syntax errors on my code.
#3

[eluser]Dam1an[/eluser]
Hi, welcome to CI

Take a look at Hooks
They let you 'hook' into the CI core at various points, such as post_controller which will get called after the constructor and functions have been executed

You'll need to enable them in the config file, but it's all explained in the user guide

Edit: Damn, Fuzz beat me, although he didn't steal my answer
Now you have 2 possibile solutions to choose from
#4

[eluser]hightower[/eluser]
Thanks guys Smile
#5

[eluser]TheFuzzy0ne[/eluser]
[quote author="Dam1an" date="1246300698"]Edit: Damn, Fuzz beat me, although he didn't steal my answer[/quote]

You say that like you didn't see it coming. Tongue
#6

[eluser]Phil Sturgeon[/eluser]
The reason __destruct() doesn't load the output in this case is that it will only be fired off when the class is finished doing its work. Seeing as controllers extend Controller, which extends CI_Base, which extends CI_Loader, its work is never done until the grandaddy instance is dead. By this time, output library will be inaccessible and nothing will be output.
#7

[eluser]Phil Sturgeon[/eluser]
Thinking about it, you could do this:

Code:
class Welcome extends Controller()
{
    function index()
    {
        $this->load->view('content');
    }

    function something_else()
    {
        $this->load->view('something_else');
    }

    function _output($output)
    {
        $this->load->view('header', $this->data, TRUE);
        echo $output;
        $this->load->view('footer', $this->data, TRUE);
    }

}

Let me know how that works out for you. The CodeIgniter user guide does mention:

Quote:Please note that your _output() function will receive the data in its finalized state. Benchmark and memory usage data will be rendered, cache files written (if you have caching enabled), and headers will be sent (if you use that feature) before it is handed off to the _output() function. If you are using this feature the page execution timer and memory usage stats might not be perfectly accurate since they will not take into account any further processing you do. For an alternate way to control output before any of the final processing is done, please see the available methods in the Output Class.




Theme © iAndrew 2016 - Forum software by © MyBB