1

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.

Community
  • 1
  • 1
jdiz414
  • 13
  • 1
  • 4

2 Answers2

1

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.

marlonjd
  • 135
  • 10
  • Thanks, but I'm getting the same error: *Call to undefined method CI_Loader::load_template()* I don't think the issue is with the load_template() code--I don't think the system is finding the MY_Loader module in the first place. – jdiz414 Feb 17 '17 at 14:24
  • Are you using CodeIgniter2 ? Could you try to add "MY_loader" to application/config/autoload.php on the libraries section – marlonjd Feb 17 '17 at 16:15
  • İf it didnt work again. Try to use helper (for example Login_helper.php in application/helpers folder) and load with helper with autoloader in config folder ($autoload['helper'] = array('Login');) then call function. İf you still need help İ could add these on answer – marlonjd Feb 17 '17 at 16:52
  • @jdiz414 check in config.php which value has $config['subclass_prefix'] it was NEW_ for me, not MY_ and correct it in file name or config – Eugene Mar 29 '21 at 14:20
1

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';
Narf
  • 14,600
  • 3
  • 37
  • 66