GMgKe586q6suSQnyqZLlGCooeWM

Pages

Search

Showing posts with label CakePHP. Show all posts
Showing posts with label CakePHP. Show all posts

Tuesday, April 5, 2011

CakePHP - Metadata Digging

On view file, you can show the metada on the CakePHP:
<?php
var_dump($this);
var_dump($form);
var_dump($html);
?>
Share/Bookmark

Monday, April 4, 2011

CakePHP - Creating Elements

Elements is a lightweight html component. It lies on the app\view\elements\hello.ctp
Then on the view, you can access it by
<?php echo $this->element('hello'); ?>
And you can also pass the value from the view to the element by second arguments, like this one:
<?php echo $this->element('hello',array('name'=>'Lady Gaga'));?>
Share/Bookmark

CakePHP - Creating Layout

Layout is a part of presentation side of CakePHP. Just copy the default.ctp in cake/libs/view/layouts/default.ctp to app/views/layout/default.ctp
The customize it:
1. Loading title:
<?php echo $title_for_layout;?>
2. Loading content
<?php echo $content_for_layout;?>
3. Loading css file that lies on webroot/css/some.css
<?php echo $html->css('filname_without_extenstion');?>
4. Loading javascript file that lies on webroot/js/some.js
    a. In controller file, add var $helpers = array('Javascript');
    b. In the layout, add <?php echo $javascript->link('filename_without_ext');?>
Share/Bookmark

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