GMgKe586q6suSQnyqZLlGCooeWM

Pages

Search

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
<?php

class PostsController extends AppController{
    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());
        $this->set('hello','This comes from bill');
    }
}

4. Make model file E:\xampp\htdocs\cakephp\app\models\post.php
<?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>
<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 $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>
 

    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;?>

Share/Bookmark

No comments:

Post a Comment