Hello,

I wonder where to place include in Codeigniter.

for example:

views/vdepan.php

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

where should I place navigation.php file ?

Recommended Answers

All 8 Replies

Codeigniter is designed to be an MVC framework which means:

model layer: read/writes to the database
view layer: the website's front-end skin
controller layer: retrieves data from the model layer and preps it for the view layer

You shouldn't really be including other php files in your views. However, if you're just trying to do a shared navigation menu, I guess that's alright. Put it wherever you want your navigation menu to be. All of the views should primarily just be templates.

I totally agree with Dani. All things can be either assigned in your controller or inherited from the view pages...

On your previous questions, there was some controller codes that look similar to this..I don't remember them exactly, but I would guess it was something like this.

public function index()
{
    $data['news'] = $this->news_model->get_news();
    $data['title'] = 'Some title';

$this->load->view('templates/header', $data);
$this->load->view('news/index', $data);
$this->load->view('templates/footer');

So, if we want the navigation items on the header file, then put it there, If it is coming from the database, then load it through the controller..

for example,. we want a navigation item for the page..

 public function index()
{
    $data['news'] = $this->news_model->get_news();
    $data['title'] = 'Some title';

    ## assign things going for the headers
    $header['home'] = 'view/home';
    $header['about'] = 'view/about';
    $header['register'] = 'view/register';
    $header['title'] = 'Title';

$this->load->view('templates/header', $header);
$this->load->view('news/index', $data);
$this->load->view('templates/footer');

The file templates/header.php can use the item, just by

 <a href="<?php echo $home;?>">Home</a>
  <a href="<?php echo $about;?>">About Us</a>

You can also asign the base url for specific controller files like

  $config["base_url"] = base_url().'index.php/news';

MVC frameworks are very powerful tool, you just have to know their basic consruct.. otherwise you will be lost..

Once you've learned one of them, pretty much the rest are not too hard to understand, because they are almost all the same. Except, for the symfony it has some wicked derivatives that really need some close attention and learning.

By the time, you get into pagination with CI, you probably expert in routing all these files. Pagination in CI is pretty easy thing to do..

If I place the include in views/includes/navigation.php

will this code good enough ?

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

we cannot do that in CodeIgniter and other MVC frameworks... The right way of doing this is by using the $this->load->view syntax.

For example, if you want the navigation included in the header, as long as the includes directory is inside the views directory, the autoloader will look for the file defined as parameter..

Example head.php, wants to include the includeds/navigation.php . The proper syntax or construct for it will be..

filename: head.php

    <?php $this->load->view('includes/navigation');?>

Say, you have left and right pages for the main page, just do the same..

<?php $this->load->view('includes/left');?>
<h2> This is my middle div content</h2>

<?php $this->load->view('includes/right');?>

In MVC the syntax rules always follows the PSR,.. simirlarly, symfony and Zend takes it to the next level, a PSR, composer type autoloading e.g.

  includes\navigation\navigation

which translate to the directory, namespace and the class name of the navigation file.

I replace the include codes with:

<?php $this->load->view->('includes/navigation.php'); ?>

Then this error appears:

Parse error: syntax error, unexpected '(', expecting identifier (T_STRING) or variable (T_VARIABLE) or '{' or '$' in C:\xampp\htdocs\IndonusaCI\application\views\index.php on line 25

That's line 25

I made a correction on my original response.. I got carried away hitting the gt

it should read

     <?php $this->load->view('includes/navigation'); ?>

There should be no .php file extension. the loader will take care of it for you..

ok, it now works. Now I can only see a blank screen:

blank screen

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.