Friday, August 9, 2013

How to get your first CakePHP tutorial work on WAMP?

In getting first CakePHP example works on WAMP, steps below illustrated how I get through.

1. Download CakePHP framework from GitHub: https://github.com/cakephp/cakephp/tags
2. Unzip CakePHP source code and place it in
"[WAMP_INSTALL_PATH]\wamp\www\cakephp" directory.
3. Create Post.php in "/app/Model" directory.
<?php
    class Post extends AppModel {
  
    }
?>
4. Create PostsController.php in "/app/Controller" directory.
<?php
class PostsController extends AppController {
    public $helpers = array('Html', 'Form');
 
  public function index() {
        $this->set('posts', $this->Post->find('all'));
    }
}
?>
5. Create index.ctp in "/app/View" directory.
<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; ?>
    <?php unset($post); ?>
</table>

Note: To get this example to work on WAMP, uncomment "LoadModule rewrite_module modules/mod_rewrite.so" in httpd.conf file. Restart Apache Web Server.

6. Type in "http://localhost/cakephp/posts/index" in your browser to access this example.

Once able to get the first example to work, please follow with the example provided in CakePHP website to continue with the further exploration.

No comments: