1
votes

Have copy pasted the code from Blog tutorial in cakephp 2.2 but its not working. Getting the following errors. Notice (8): Use of undefined constant Html Notice (8): Use of undefined constant Form Notice (8): Use of undefined constant posts Notice (8): Use of undefined constant all Notice (8): Undefined index: all Below is the code for PostsController and index.ctp.

<?php
class PostsController extends AppController {
public $helpers = array(’Html’, ’Form’);

public function index() {

  $this->set(’posts’, $this->Post->find(’all’));

}

public function view($id = null) {
      $this->Post->id = $id;
      $this->set(’post’, $this->Post->read());
  }
}
?>

index.ctp
 <h1>Blog posts</h1>
 <table>
 <tr>
 <th>Id</th>
 <th>Title</th>
 <th>Created</th>
 </tr>
 <?php foreach ($posts as $post): ?>
 <tr>
 <td><?php echo $post[’Post’][’id’]; ?></td>
 <td>
 <?php echo $post[’Post’][’title’]; ?>
 </td>
 <td><?php echo $post[’Post’][’created’]; ?></td>
 </tr>
 <?php endforeach; ?>
 <?php unset($post); ?>
 </table>
2

2 Answers

3
votes

dont use ’ as they are not php syntax (used only in mysql)

use ' instead (simple apostrophe):

$this->set('posts', $this->Post->find('all'));

furthermore <?php unset($post); ?> is unnecessary, you should escape your output echo h($post[’Post’][’title’]) and you should omit the closing tag ?> in your php files (not in the view files, though)

0
votes

I was having problems with this for a while as well. To address this potential problem for OS X Mavericks users:

Mavericks comes with a setting to use smart quotes which makes all simple apostrophes into starting and ending apostrophes. If you go into System Preferences->Keyboard->Text->UNCHECK Use Smart quotes and dashes, you'll be able to write a normal simple apostrophe. This solved the problem for me.

Please note that in Mavericks, even textedit will edit your apostrophe from being a simple on to an opening and closing one.