0
votes

I have built a website with the CodeIgniter framework.

Unfortunately, I am having a problem with CodeIgniter's load->view() function.

Every time I try to load another view file with the code $this->load->view('any_view_file'); I get the error:

unable to load the requested file 'any_view_file'.php

For example i am having problems with the $this->load->view('available_lessons.php', $data); command. So let's try to recreate the problem and think this through...

  1. I am calling the load_available_lessons() function of the Lessons.php controller
  2. The load_available_lessons() function uses the get_lessons() function of the db_model.php model and saves the result on the $data['result'].
  3. Then, the load_available_lessons() function calls the available_lessons view file while parsing the $data to it.
  4. Here is where the problem rises. CodeIgniter is unable to load the view. As i explained earlier, CodeIgniter fails to load any view...!!!

Any suggestions please?

My .htaccess file:

RewriteEngine on  
RewriteCond $1 !^(index\\.php|images|css|lessons|robotos\\.txt)  
RewriteCond %{REQUEST_FILENAME} !-f  
RewriteCond %{REQUEST_FILENAME} !-d  
RewriteRule ^(.*)$ index.php?/$1 [L,QSA]  

From my config.php file:

$config['index_page'] = '';

From my routes.php file:

$route['default_controller'] = 'pages/view';
$route['(:any)'] = 'pages/view/$1';
$route['translate_uri_dashes'] = TRUE;

My Pages.php Controller:

<?php
class Pages extends CI_Controller {

        public function view($page = 'home')
        {
                if ( ! file_exists(APPPATH.'views/pages/'.$page.'.php'))
                {
                        // Whoops, we don't have a page for that!
                        show_404();
                }
                else
                {
                $data['title'] = ucfirst($page); // Capitalize the first letter

                $this->load->view('templates/header', $data);
                $this->load->view('pages/'.$page, $data);
                $this->load->view('templates/footer', $data);
                }
        }
}
?>

My Lessons.php Controller:

<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Lessons extends CI_Controller {

        public function __construct()
        {
                parent::__construct();
                $this->load->model('db_model');
                /* $this->load->library('acl'); */
                /* $this->load->helper(array('form', 'url')); */
        }

        public function index()
        {
                $this->load->view('available_lessons', array('error' => ' ' ));
        }

        public function load_available_lessons()
        {
            $data['result'] = $this->db_model->get_lessons();
            $this->load->view('available_lessons.php', $data); /* available_lessons */
        }
}
?>

My db_model.php Model:

<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class db_model extends CI_Model{

        public function __construct()
        {
                parent::__construct();
        }


        public function new_lesson($data)
        {
            /* Inserting in Table "lessons" of CodeIgniter's Database */
            $this->db->insert('available_lessons', $data);
        }

        public function get_lessons() /* $data */
        {
            /* Get lessons from Table "lessons" of CodeIgniter's Database */
            $this->db->select('Name, Description, DateAdded, LastEdit, UserID, FileName');
            $this->db->from('lessons');
            $query = $this->db->get();
            return $result = $query->result();
        }
}

?>

My available_lessons.php View file:

<?php
defined('BASEPATH') OR exit('No direct script access allowed');
?>

<div class="container">
  <h6 class="description">Browse Lessons</h6>
  <div class="row">
    <section id="lessons-panel" class="col-xs-12 col-md-8 col-md-offset-2">
        <table>
        <tr>
        <th>Content</th>
        </tr>
        <?php foreach($this->result  as $r): ?>
        <tr><?php echo $r->content; ?>

            </tr>
        <?php endforeach; ?>
        </table>
    </section>
  </div>
</div>
1
If your any_view_file.php in a subdirectory under /views you need to do $this->load->view('subdir/any_view_file.php', ''); - ourmandave
@ourmandave if view is a php file you don't need to include .php codeigniter.com/user_guide/general/views.html#loading-a-view $this->load->view('subdir/any_view_file'); - Mr. ED
@ourmandave No, it is not in a subdirectory. It is just inside the view folder. - Anastasios Giannaros
@wolfgang1983 Yes, i think i have named them correctly. Thank you all for your answers!!!!!!!!!!!!! - Anastasios Giannaros

1 Answers

0
votes

As @ourmandave and a coworker pointed out to me, i should add the "pages/" before every view name that i want to call. For example, in the Lessons.php controller, i should have $this->load->view('pages/available_lessons.php', $data); As it turned out he is right. The thing is that i cannot understand the reason why i should include the "pages/" as I have already included this in the Pages.php controller. It seems that the Pages Controller is not being used when i call a view. I will have to look into it later on. Thank you all for your valuable input!!!!! Just another rookie mistake...