3
votes

I am getting this error on my first attempt of CakePHP:

Undefined variable: html [APP/View/Posts/index.ctp, line 13]

I have version 2.0-alpha, have I got the wrong version or what has changed again. Seems it can't find the html helper.

More info as requested:

Here is the index.ctp file

<?php foreach ($posts as $post): ?>    

<?php echo $post['Post']['id']; ?>


<?php
##line 13 here
 echo $html->link($post['Post']['title'], array('controller' => 'posts', 'action' => 'view', $post['Post']['id'])); 
?>


<?php echo $post['Post']['created']; ?>

<?php endforeach; ?> 

The data is definitely coming through but the error I get is this on line 13:

Undefined variable: html [APP/View/Posts/index.ctp, line 13] Fatal error: Call to a member function link() on a non-object in /home

I'm quite new I hope this helps.

Update 5hrs later of going crazy

Thanks guys it's sorted incase anybody has this problem the tutorial on the main site is an old one and nobody has made the effort to update it!! ...in the index.ctp example replace

$html->link(... 

with

$this->Html->link(...
3
That's just a warning. Most likely you've got something like $var = $undefined_var + 1; and the warning is due to the $undefined_var. - Marc B
Without showing some code, it's hard for somebody to help you. Please give some more details about what you are trying to do. - Ikke
Sorry here is the code in the ctp file - Ola
I have added more info see the post above - Ola
You might be following an outdated tutorial that uses ver 1.2 conventions. If you're new to Cake I would recommend that you stick with the stable 1.3 release for now. It's well documented and you can later upgrade to 2.0 when it has matured enough. - JJJ

3 Answers

9
votes

From the manual of cakephp, it seems that $html should be $this->Html in CakePHP 2.0.

0
votes

Just make this change:

<?php
  ##line 13 here
  echo $this->html->link($post['Post']['title'], array('controller' => 'posts', 'action' => 'view', $post['Post']['id'])); 
?>
0
votes

From CakePHP 2.0 all Helpers are called on class (this) and with a first capital letter as standard $this->Html-> (Html). Same for Form Helper and such.