Welcome Guest, Not a member yet? Register   Sign In
__destruct and outputting views
#1

[eluser]TheLizardKing[/eluser]
I am trying to output a view using PHP5's __destruct in an attempt to create a header/content/footer setup.

Code:
<?php

class Links extends Controller {
  function __construct() {
    parent::Controller();

    $this->load->view('common/header');
  }
  
  function index() {
    $this->hot();
  }
  
  function hot() {
    $this->load->view('links/hot');
  }
  
  function __destruct() {
    $this->load->view('common/footer');
  }
}

When ran my output is always the content of common/header.php then links/hot.php then nothing. I can echo out text in my destructor but my view never outputs. I know the view is good because I can place it anywhere else. Am I mistaken on how the destructor works? What is a good work around?
#2

[eluser]drewbee[/eluser]
I think your problem is the fact that the views output is ran before __destruct() happens.
You will have to force Code Igniter to not use its own output, and rather force it out on your own. I know how to output code igniters current setup, however telling it not to output its own data is a mess regardless.


Code:
function __destruct()
{
    $this->load->view('common/footer');
    echo $this->output->get_output();
    die();
}

Why not load the footer view within the links/hot.

This way you are not calling this in your controller, but the view ie

links/hot.php (view)
Code:
echo $this->load->view('header', TRUE);
echo 'this is my hot links view';
echo $this->load->view('footer', TRUE);

I am not sure if that will work or not, but that would be my best method for doing this.

Personally, I just create one global_view file.
#3

[eluser]TheLizardKing[/eluser]
HMM, I think I'll use your header/footer in the view advice. Thanks!
#4

[eluser]nikefido[/eluser]
isn't destruct only called when an object is "destroyed" ?
#5

[eluser]TheLizardKing[/eluser]
Well destroyed or execution of the object is complete. You can test it by throwing an echo into the destructor and seeing if it outputs.




Theme © iAndrew 2016 - Forum software by © MyBB