CodeIgniter Forums

Full Version: PHP includes aren't working in CI
You're currently viewing a stripped down version of our content. View the full version with proper formatting.

El Forum


I'm basically trying to include some common code in my View files (like the navigation, footer...etc), but every time I include them, I get an error saying that PHP couldn't open the file, even though it's actually there. If I do this out of CodeIgniter, it works just fine.

Is there anything I should change in CI before I could use includes or something?

I'm including files from the root directory of my site by the way.

Appreciate your help Smile

El Forum

[eluser]Jamie Rumbelow[/eluser]
Don't. CodeIgniter hates sending out headers before the output class is processed. If you need to have some information, turn it into a library or a model.



El Forum

How does that work? I'd rather not have to include code in all of my classes and their methods again. I'd like to control the includes from the view files.

El Forum

[eluser]Jamie Rumbelow[/eluser]
The whole system builds a string of all the things in your views, and sends that to the browser. You can manually override that with the $this->output->set_output() method. Do something like this as a hook maybe:

$this->output->set_output(file_get_contents("") . $this->output->get_output());

Read more about the output class here:

El Forum

So, if I want to include a navigation somewhere in all my views, how do I do that through the controller without modifying all of my application's methods?

El Forum

[eluser]Derek Allard[/eluser]
Another thing to consider... you can put views in views. At the top of your content view, you can include
... // rest of site
if you wanted.

El Forum

Thanks Derek. This is just how I was hoping I could include components into my views.

However, it appreas that the code of the views I include can't see the variables of the the view they're being added to.

For example:
$date = '25/05/08';
// the 'something' page needs to use the $date variable, but gets an "Undefined Variable" notice
How do I avoid that?

El Forum

[eluser]Derek Allard[/eluser]
variables would still need to be created and passed to the views in the normal way.
$vars['the_date'] = "25/05/08";
$this->load->view('includes/something', $vars);
This could (and probably should) also be passed from the controller.

El Forum

[eluser]Michael Wales[/eluser]
function index() {
  $vars['the_date'] = date('Y-m-d');
  $vars['view'] = 'home/photos';
  $this->load->view('view', $vars);


All of those views (header, sidebar, footer, the view passed via a var (home/photos) will have access to the entire array of variables (they would be able to echo the date, perfectly fine).

El Forum

I had a similar problem and a nice person in the IRC channel pointed me at the Django-like template inheritance helper (found here).

Say if you have a view called 'base.php' with:
    <title>Wooshy -  <? start_block_marker('title') ?>Home<? end_block_marker() ?></title>

<h1>&lt;? start_block_marker('heading') ?&gt;Home&lt;? end_block_marker() ?&gt;</h1>
<ul id="navigation">
&lt;? start_block_marker('content') ?&gt;
&lt;? end_block_marker() ?&gt;

and in your 'about.php' view you have:

echo $title;

echo $heading;

echo "<p>Some text</p>"


You can simply load the 'about.php' view and it will automatically inherit the html from base.php.
$vars['title'] = "About us";
$vars['heading'] = "All about our nice team";

Nice or what Smile Hope this may help.