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...
- I am calling the load_available_lessons() function of the Lessons.php controller
- The load_available_lessons() function uses the get_lessons() function of the db_model.php model and saves the result on the $data['result'].
- Then, the load_available_lessons() function calls the available_lessons view file while parsing the $data to it.
- 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>
any_view_file.phpin a subdirectory under /views you need to do$this->load->view('subdir/any_view_file.php', '');- ourmandave.phpcodeigniter.com/user_guide/general/views.html#loading-a-view $this->load->view('subdir/any_view_file'); - Mr. ED