Saturday, April 2, 2011

CakePHP - The Structure

When you are ready to build an application after simple setting of configuration, do this:
1. Create a table name like posts
    id int not null auto_increment key,
    title char(50),
    body text,
    created datetime,
    modified datetime

2. The insert some data to it
3. Make controller file in E:\xampp\htdocs\cakephp\app\controllers\posts_controller.php

class PostsController extends AppController{
    var $name = 'Posts';
    function index(){
    function view($id=null){
        $this->Post->id = $id;
        $this->set('hello','This comes from bill');

4. Make model file E:\xampp\htdocs\cakephp\app\models\post.php
class Post extends AppModel{
    var $name = 'Post';
5. Make the view file:
    a. index.ctp in E:\xampp\htdocs\cakephp\app\views\posts\index.ctp

<h1>Blog posts</h1>
<!-- Here is where we loop through our $posts array, printing out post info -->
<?php foreach ($posts as $post): ?>
<td><?php echo $post['Post']['id']; ?></td>
 <?php echo $html->link($post['Post']['title'],
array('controller' => 'posts', 'action' => 'view', $post['Post']['id'])); ?>
 <td><?php echo $post['Post']['created']; ?></td>
<?php endforeach; ?>

    b. view.ctp E:\xampp\htdocs\cakephp\app\views\posts\view.ctp

<h1><?php echo $post['Post']['title']?></h1>
<p><small>Created: <?php echo $post['Post']['created']?></small></p>
<p><?php echo $post['Post']['body']?></p>

<?php print_r($post);?>
<?php print $hello;?>


No comments:

Post a Comment