Php – codeigniter returns “Message: Undefined property: Welcome::$load” trying to load helper lib

codeigniterPHPurl

enter image description here

Background Information

I just installed a fresh copy of CI and modified the welcome controller to include the url Helper so I can call the method base_url. I then try to call this method from home.php

Problem:
I'm getting the following error message:

Message: Undefined property: Welcome::$load
Filename: controllers/welcome.php

Code:

This is what my welcome controller now looks like:

class Welcome extends CI_Controller {
    public function __construct()
    {
        $this->load->helper('url');     
    }   

    public function index()
    {
        $this->load->view('home');
    }
}

The view looks like this:

<!DOCTYPE html> 
<html lang="en">
  <head>
    <meta http-equiv="X-UA-Compatible" content="IE=Edge"/>
    <meta charset="utf-8">
    <meta name="viewport" content="width = device-width">    
    <meta name="description" content="">
    <!-- Le styles -->    
    <title>test site</title>
    <script>   
      var BASEPATH = "<?php echo base_url(); ?>";
    </script>
    <link href="<?php echo base_url('assets/css/bootstrap.min.css')?>" rel="stylesheet">
    <link href="<?php echo base_url('assets/css/navbar.css')?>" rel="stylesheet">    
  </head>

The system is dying on the line in the controller's constructor where i try to load the library…

What I've done so far:

  1. Read the manual. https://www.codeigniter.com/user_guide/helpers/url_helper.html
  2. Tried to include the url library in the config/autoload.php like so:

    $autoload['helper'] = array('url');

But I'm still getting the error.
Any suggestions?

Thanks.

Screenshots:

Best Answer

You forgot a crucial thing;

class Welcome extends CI_Controller {
    public function __construct()
    {
        parent::__construct();
        $this->load->helper('url'); //Loading url helper    
    }   

    public function index()
    {
        $this->load->view('home'); //Loading home view
    }
}

The parent::__construct. If you don't do that; the Controller will not inherit it's abstract layer when you override the __construct in your own controller.

As long as you don't override your __construct it's all ok. It only happens when you override it. You don't have the load functionality because the Welcome class is empty (no inheritance), even if it extends the CI_Controller (but with a __construct override).

Related Topic