CodeIgniter Forums
Message: Undefined property: Start::$config - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forum-20.html)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forum-23.html)
+--- Thread: Message: Undefined property: Start::$config (/thread-54077.html)



Message: Undefined property: Start::$config - El Forum - 08-21-2012

[eluser]Naczu[/eluser]
Hi. I am newbie. I have problem. I use the latest version 2.1.2 of codeigniter. I have a controller which is start.php like this.

Code:
<?php
class Start extends CI_Controller
{
var $base;
var $css;
function Start()
{
$this->base = $this->config->item('base_url');
$this->css = $this->config->item('css');
}
function hello($name = 'Guest')
{
$data['css'] = $this->css;
$data['base'] = $this->base;
$data['mytitle'] = 'Welcome to this site';
$data['mytext'] = "Hello, $name, now we're getting dynamic!";
$this->load->view('testview', $data);
}
}

and testview.php file is

Code:
<html>
<head>
<title>Web test Site</title>
<link rel="stylesheet" type="text/css" href="<?php echo
$base."/".$css;?>">
</head>
<body>
<h1>&lt;?php echo $mytitle; ?&gt; </h1>
<p class='test'> &lt;?php echo $mytext; ?&gt; </p>
&lt;/body&gt;
&lt;/html&gt;


and in config.php

Code:
$config['base_url'] = 'http://localhost/codeigniter/';
$config['css'] = 'css/style.css';

I got this error...

A PHP Error was encountered

Severity: Notice

Message: Undefined property: Start::$config

Filename: controllers/start.php

Line Number: 8

Fatal error: Call to a member function item() on a non-object in C:\xampp\htdocs\codeigniter\application\controllers\start.php on line 8

where is the problem ???? Please help me


Message: Undefined property: Start::$config - El Forum - 08-21-2012

[eluser]timmahoney[/eluser]
I think you need to add this to the top of your controller, in order to get all the stuff CodeIgniter provides

Code:
public function __construct()
{
  parent::__construct();
}



Message: Undefined property: Start::$config - El Forum - 08-21-2012

[eluser]Naczu[/eluser]
omg yes it worked ... but why O_O

ok .. it worked but I couldnt get the $css and $base variables... what is wrong with the start.php controller ?

[quote author="timmahoney" date="1345572814"]I think you need to add this to the top of your controller, in order to get all the stuff CodeIgniter provides

Code:
public function __construct()
{
  parent::__construct();
}
[/quote]


Message: Undefined property: Start::$config - El Forum - 08-21-2012

[eluser]timmahoney[/eluser]
The __construct method automatically runs when the Controller is loaded, and the line parent::__construct() is running the __construct in the CI_Controller class, (the parent) so it's loading all the CodeIgniter stuff like $this->config


Message: Undefined property: Start::$config - El Forum - 08-21-2012

[eluser]Naczu[/eluser]
ok .. it worked but I couldnt get the $css and $base variables on the testview.php … what is wrong with the start.php controller ?



Message: Undefined property: Start::$config - El Forum - 08-21-2012

[eluser]timmahoney[/eluser]
The "Start" method you have in there is not getting run automatically. Try this:
Code:
&lt;?php
class Start extends CI_Controller
{
var $base;
var $css;
function __construct()
{
  parent::__construct();
  $this->base = $this->config->item('base_url');
  $this->css = $this->config->item('css');
}
function hello($name = 'Guest')
{
  $data['css'] = $this->css;
  $data['base'] = $this->base;
  $data['mytitle'] = 'Welcome to this site';
  $data['mytext'] = "Hello, $name, now we're getting dynamic!";
  $this->load->view('testview', $data);
}
}



Message: Undefined property: Start::$config - El Forum - 08-21-2012

[eluser]Naczu[/eluser]
aww thank you so much ... Smile you solved my problem .


Message: Undefined property: Start::$config - El Forum - 08-21-2012

[eluser]timmahoney[/eluser]
Glad I could help!