![]() |
PHP includes aren't working in CI - Printable Version +- CodeIgniter Forums (https://forum.codeigniter.com) +-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20) +--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forumdisplay.php?fid=23) +--- Thread: PHP includes aren't working in CI (/showthread.php?tid=8632) |
PHP includes aren't working in CI - El Forum - 05-25-2008 [eluser]KeyStroke[/eluser] Hi, 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 ![]() PHP includes aren't working in CI - El Forum - 05-25-2008 [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. Thanks, Jamie PHP includes aren't working in CI - El Forum - 05-25-2008 [eluser]KeyStroke[/eluser] 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. PHP includes aren't working in CI - El Forum - 05-25-2008 [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: Code: $this->output->set_output(file_get_contents("include.inc") . $this->output->get_output()); Read more about the output class here: http://ellislab.com/codeigniter/user-guide/libraries/output.html PHP includes aren't working in CI - El Forum - 05-25-2008 [eluser]KeyStroke[/eluser] 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? PHP includes aren't working in CI - El Forum - 05-25-2008 [eluser]Derek Allard[/eluser] Another thing to consider... you can put views in views. At the top of your content view, you can include Code: $this->load->view('common_header'); PHP includes aren't working in CI - El Forum - 05-25-2008 [eluser]KeyStroke[/eluser] 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: Code: <?php PHP includes aren't working in CI - El Forum - 05-25-2008 [eluser]Derek Allard[/eluser] variables would still need to be created and passed to the views in the normal way. Code: $vars['the_date'] = "25/05/08"; PHP includes aren't working in CI - El Forum - 05-25-2008 [eluser]Michael Wales[/eluser] controller/home.php Code: function index() { views/view.php Code: $this->load->view('_global/header'); 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). PHP includes aren't working in CI - El Forum - 05-25-2008 [eluser]Unknown[/eluser] 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: Code: <html> and in your 'about.php' view you have: Code: <?php You can simply load the 'about.php' view and it will automatically inherit the html from base.php. Code: $vars['title'] = "About us"; Nice or what ![]() |