Reputation: 28853
I've just been following the tutorial on the CakePHP website to create a simple Blog as a way to learn a bit about Cake. However I have run into an error and not sure why as I have followed exactly what the tutorial says. The errors:
Notice (8): Undefined property: View::$Html [APP/views/posts/index.ctp, line 17]
Fatal error: Call to a member function link() on a non-object in /Users/cameron/Sites/dentist/app/views/posts/index.ctp on line 17
Here is my posts_controller
<?php
class PostsController extends AppController {
var $helpers = array('Html', 'Form');
var $name = 'Posts';
function index() {
$this->set('posts', $this->Post->find('all'));
}
function view($id = null) {
$this->Post->id = $id;
$this->set('post', $this->Post->read());
}
}
?>
and here is my model
<?php
class Post extends AppModel {
var $name = 'Post';
}
?>
and here are my views
<!-- File: /app/views/posts/index.ctp -->
<h1>Blog posts</h1>
<table>
<tr>
<th>Id</th>
<th>Title</th>
<th>Created</th>
</tr>
<!-- Here is where we loop through our $posts array, printing out post info -->
<?php foreach ($posts as $post): ?>
<tr>
<td><?php echo $post['Post']['id']; ?></td>
<td>
<?php echo $this->Html->link($post['Post']['title'], array('controller' => 'posts', 'action' => 'view', $post['Post']['id'])); ?>
</td>
<td><?php echo $post['Post']['created']; ?></td>
</tr>
<?php endforeach; ?>
</table>
Update
Turns out I was using an older version of CakePHP.
Upvotes: 0
Views: 2047
Reputation: 6340
From http://book.cakephp.org/view/1572/New-features-in-CakePHP-1-3:
Helpers can now be addressed at $this->Helper->func() in addition to $helper->func(). This allows view variables and helpers to share names and not create collisions.
You're using a version of CakePHP below 1.3.
Upvotes: 2
Reputation: 7516
Instead of this line in your view,
<?php echo $this->Html->link($post['Post']['title'], array('controller' => 'posts', 'action' => 'view', $post['Post']['id'])); ?>
use this to call the helpers:
<?php echo $html->link($post['Post']['title'], array('controller' => 'posts', 'action' => 'view', $post['Post']['id'])); ?>
Upvotes: 0