Welcome Guest, Not a member yet? Register   Sign In
Autoloading Libraries
#1

[eluser]Unknown[/eluser]
Hi,I've got a library called Layout and I'm trying to load it from autoload.php
then from the controller main.php I'm trying to use this library that should be automatically loaded, but it isn't it just shows this error:

Code:
A PHP Error was encountered

Severity: Notice

Message: Undefined property: Main::$layout

Filename: controllers/main.php

Line Number: 10

Fatal error: Call to a member function view() on a non-object in /var/www/system/application/controllers/main.php on line 10

autoload.php :
Code:
$autoload['libraries'] = array('database','session', 'layout');

main.php:
Code:
class Main extends Controller {

    function __construct(){
        parent::Controller();
        //$this->load->library('layout'); - if I uncomment this and remove from autoload.php it works otherwise it doesn't
    }
    function index(){
        $this->layout->view('dashboard');
    }
}

application/libraries/Layout.php :

Code:
class Layout {
    
    private $obj;

    public function __construct(){
        $this->obj = & get_instance();
    }
    
    /**
     *
     *  Loads a main template and replaces the content var inside it with $template
     *  
     * @param $template String
     * @param $data Array[optional]
     * @param $return Boolean[optional]
     * @param $baseView String[optional]
     * @return String
     */
    public function view($template, $data = array(), $return = false ,  $baseView = "main") {
        return $this->obj->load->view($baseView, array("content" => $this->obj->load->view($template, $data, true)), $return);
    }
}

CI Version: 1.6.2




Theme © iAndrew 2016 - Forum software by © MyBB