article

Sunday, August 4, 2013

CakePHP Making a small application

CakePHP Making a small application

Create Database Table

CREATE TABLE `categories` (
  `id` INTEGER(11) NOT NULL AUTO_INCREMENT,
  `name` VARCHAR(255) NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=MyISAM
Create Model app\models\category.php 


<?php
class Category extends AppModel {
    var $name = 'Category';
}
?>
Create Controller app\controllers\categories_controller.php
<?php
class CategoriesController extends AppController {
    var $name = 'Categories';
    function index() {
        $this->set('categories', $this->Category->find('all'));
    }
     
    function add() {
        if (!empty($this->data)) {
            if ($this->Category->save($this->data)) {
                $this->Session->setFlash('Your category has been saved.');
                $this->redirect(array('action' => 'index'));
            }
        }
    }
}
?>
Create View index app\views\categories\index.ctp
<!-- File: /app/views/categories/index.ctp -->
 <?php echo $html->link('Add Category',array('controller' => 'categories', 'action' => 'add')); ?>
<h1>Categories</h1>

<table>
    <tr>
        <th>Id</th>
        <th>Title</th>
    </tr>
 
    <?php foreach ($categories as $category): ?>
    <tr>
        <td><?php echo $category['Category']['id']; ?></td>
        <td>
            <?php echo $html->link($category['Category']['name'],
array('controller' => 'categories', 'action' => 'view', $category['Category']['id'])); ?>
        </td>
    </tr>
    <?php endforeach; ?>
 
</table>
Create View add app\views\categories\add.ctp
<!-- File: /app/views/categories/add.ctp -->   
<h1>Add Category</h1>
<?php
echo $form->create('Category');
echo $form->input('name');
echo $form->end('Save Post');
?>

Related Post