1

i have a problem including a folder/file in codeigniter, this is how the files are structured in my view.

-application
  -model
  -controller
  -view
    -auth
      -login.php  //I want to include header.php, sidebar and footer on this file
    -includes
      -header.php
      -sidebar.php
      -footer.php

on the application/view/auth/login.php i have include('../../include/header'); but is not working, i have tried several other means and no job also i cant use an absolute path because the files are in the application folder.

edit:

example in my view folder i have index.php file and i has the following codes

      <?php
       include('includes/header.php');
       include('includes/sidebar.php');
      ?>

       <div id='content'>
           <h3>Title</h3>
           <p>contents contents</p>

       </div>
      <?php include('/includes/footer.php'); ?>

Now in the view folder is another folder called auth and i have a file in it called login.php and has the following codes

     <?php include(../includes/header.php);?> //this doesn't work
     <?php include(../includes/sidebar.php);?> //this doesn't work

     <div id='form'>
        <form>
        </form>
     </div>

     <?php include(../includes/header);?> //this doesn't work

on my controller i have

  <?php 
  class Index extents CI_Controller
  {
   public function __construct()
     {
       parent::__construct();
     }

   public function index()
     {
      $this->load->view('index'); //this works and loads all the includes
     }
  }

another example of a controller not working

  <?php 
  class Login extents CI_Controller
  {
   public function __construct()
     {
       parent::__construct();
     }

   public function index()
     {
      $this->load->view('auth/login'); //this works but doesn't load the includes
                                       //in the login.php file found inside the auth 
                                       //folder
     }
  }
user2666633
  • 320
  • 4
  • 20

3 Answers3

2

You can use APPPATH. APPPATH is a CodeIgniter constant. It contains the value of the full path to your application folder.

<?php include(APPPATH.'views/includes/header.php');
//your code here
include(APPPATH.'views/includes/footer.php'); ?>
Alex
  • 39
  • 4
1

Take a look a the view loader (http://ellislab.com/codeigniter/user-guide/libraries/loader.html).

Most CI functions are available with inviews, so you can use something like this:

Instead of

include('../../include/header');

use

$this->load->view('includes/header');

Lorenzo Aiello
  • 497
  • 1
  • 7
  • 18
  • Thanks for but what i am tryin got do is not to load the view yet, i just want to include the header.php on a file login.php and they are both in different folders. – user2666633 Jan 03 '14 at 21:24
  • WHY are you trying includes @user2666633 in your controllers? Add your code into the controller, or use a model, OR helpers OR libraries. You are doing it wrong if you are trying an include with an **INVALID** dir anyway – Jakub Jan 03 '14 at 21:39
  • may be i didnt explain very well, let me try to thanks, i have a template (includes) which comprises of header, sidebar, footer so if i want to create a page in my view i just include the header, sidebar, footer and between it i write my content, so when i want to display this page i just call the file with the includes and content and i have my page. but now i am loading view from another folder and i still need to include my template from the includes folder. i will try to edit my post – user2666633 Jan 03 '14 at 22:21
0

This is an old post, but for as many as would stumble upon it.

I'll advice that you move the include folder to your root folder. It shouldn't be in the application folder, but the folder where the application folder is.

Then you replace with

<?php include(base_url(include/header.php)); ?>

That should do the trick.

Mina Abadir
  • 2,951
  • 2
  • 15
  • 20
pamekar
  • 729
  • 7
  • 10