Reputation: 13
I am trying to create a header / body / footer template in CodeIgniter similar to that described at: Header and footer in CodeIgniter
My code stored at \application\core\MY_loader.php:
<?php
class MY_Loader extends CI_Loader {
public function load_template($template_name, $vars = array(), $return = FALSE)
{
if($return):
$content = $this -> view('templates/header', $vars, $return);
$content .= $this -> view($template_name, $vars, $return);
$content .= $this -> view('templates/footer', $vars, $return);
return $content;
else:
$this -> view('templates/header', $vars);
$this -> view($template_name, $vars);
$this -> view('templates/footer', $vars);
endif;
}
}
?>
My controller code stored at application\controllers\managers.php:
class Managers extends CI_Controller {
function login()
{
$this -> load -> load_template('managers/login');
}
}
When I browse to BASE_URL/managers/login, I get this error:
Call to undefined method CI_Loader::load_template()
My interpretation of this is that the system is not extending CI_Loader with MY_Loader, but is instead disregarding MY_Loader entirely. This setup was working on my local install of the site when I was running it under XAMPP, but it stopped working after I ported the site to a web host. I don't remember changing the CI configuration (although I might have), nor do I know if this is due to a configuration issue at the new host.
I am looking for any guidance as to what might be preventing MY_loader from extending CI_loader. I haven't been able to find any similar reports; all the other issues I've found related to MY_loader assume that the override is already working.
Upvotes: 1
Views: 4038
Reputation: 135
Save as MY_Loader.php this (Thanks to other friend's suggestion that "L" is also has to be capital letter)
public function load_template($template_name, $vars = array(), $return = FALSE)
{
$CI = & get_instance();
if($return):
$content = $CI->load->view('templates/header', $vars, $return);
$content .= $CI->load->view($template_name, $vars, $return);
$content .= $CI->load->view('templates/footer', $vars, $return);
return $content;
else:
$CI->load->view('templates/header', $vars);
$CI->load->view($template_name, $vars);
$CI->load->view('templates/footer', $vars);
endif;
}
}
?>
I hope I could help. Have a nice day.
Upvotes: 1
Reputation: 14752
The file has to be called "MY_Loader.php" - it is case-sensitive and NOT the same as "MY_loader.php".
Contrary to the only other answer at this time, "My_loader.php" will NOT work either, as the subclass_prefix
is applied separately from the library name.
The simplest way to exemplify it is this:
$libraryName = 'loader';
$className = ucfirst(strtolower($libraryName));
$className = config_item('subclass_prefix').$className;
$fileName = $className.'.php';
Upvotes: 1