1. create database :
We’re using MySQL, as it’s the most common of the free database engines available for the movie application:
2. connectdatabase :
Open file "database.php " in path to "/var/www/cakephp/app/Config" (my project "cakephp" ) , and edit :
3. create file "MoviesController.php." in path to :" /var/www/cakephp/app/controller/", and "add" code :
4. create file "SessionComponent.php" in path to : "/var/www/cakephp/app/Controller/Component/", and "add" code :
5. create folder "Movies" in path to : "/var/www/cakephp/app/View/" .
6. create file "index.ctp" in path to : "/var/www/cakephp/app/View/Movies ", and "add" code :
7. create file "add.ctp" in path to : " /var/www/cakephp/app/View/Movies/ ", and "add" code :
8.create file "view.ctp" in path to : " /var/www/cakephp/app/View/Movies/ ", and "add" code :
10. create file "delete.ctp" in path to : " /var/www/cakephp/app/View/Movies/ ", and "add" code :
12. Video demo :
We’re using MySQL, as it’s the most common of the free database engines available for the movie application:
CREATE DATABASE linux_format_tutorial;
CREATE TABLE `movies` (
`id` CHAR(36) NOT NULL PRIMARY KEY,
`title` VARCHAR(255),
`genre` VARCHAR(45),
`rating` VARCHAR(45),
`format` VARCHAR(45),
`length` VARCHAR(45),
`created` DATETIME,
`modified` DATETIME
);
2. connectdatabase :
Open file "database.php " in path to "/var/www/cakephp/app/Config" (my project "cakephp" ) , and edit :
var $default = array(
‘driver’ => ‘mysql’,
‘persistent’ => ‘false’,
‘host’ => ‘localhost’,// domain
‘port’ => ‘’,
‘login’ => ‘root’,// id login phpmyadmin
‘password’ => ‘huy’,//pasword login phpmyadmin
‘database’ => ‘linux_format_tutorial’,//name database
‘schema’ => ‘’,
‘prefix’ => ‘’,
‘encoding’ => ‘’
);
3. create file "MoviesController.php." in path to :" /var/www/cakephp/app/controller/", and "add" code :
<?php
class MoviesController extends AppController {
public $helpers=array('Html','Form','Session');
public $components=array('Session');
public function index() {
$movies = $this->Movie->find('all');
$this->set('movies', $movies);
}
public function add() {
if (!empty($this->data)) {
$this->Movie->create($this->data);
if ($this->Movie->save()) {
$this->Session->setFlash(_('Thanh Cong'));
$this->redirect(array('action' => 'index'));
}
}else {
$this->Session->setFlash(_('Error'));
}
}
public function delete($id = null) {
if (!$id) {
$this->Session->setFlash('Invalid id for movie');
$this->redirect(array('action' => 'index'));
}
if ($this->Movie->delete($id)) {
$this->Session->setFlash('Movie deleted');
} else {
$this->Session->setFlash(__('Movie was not deleted',
true));
}
$this->redirect(array('action' => 'index'));
}
function edit($id = null) {
if (!$id && empty($this->data)) {
$this->Session->setFlash('Invalid movie');
$this->redirect(array('action' => 'index'));
}
if (!empty($this->data)) {
if ($this->Movie->save($this->data)) {
$this->Session->setFlash('The movie has been saved');
$this->redirect(array('action' => 'index'));
} else {
$this->Session->setFlash('The movie could not be saved. Please, try again.');
}
}
if (empty($this->data)) {
$this->data = $this->Movie->read(null, $id);
}
}
function view($id = null) {
if (!$id) {
$this->Session->setFlash('Invalid movie');
$this->redirect(array('action' => 'index'));
}
$this->set('movie', $this->Movie->findById($id));
}
}
4. create file "SessionComponent.php" in path to : "/var/www/cakephp/app/Controller/Component/", and "add" code :
<?php
class SessionComponent extends Component {
public function setFlash($message) {
}
}
5. create folder "Movies" in path to : "/var/www/cakephp/app/View/" .
6. create file "index.ctp" in path to : "/var/www/cakephp/app/View/Movies ", and "add" code :
<div class="movies index" >
<h2>Movies</h2>
<table cellpadding="0" cellspacing="0">
<tr>
<th>Title</th>
<th>Genre</th>
<th>Rating</th>
<th>Format</th>
<th>Length</th>
<th class="actions">Actions</th>
</tr>
<?php foreach ($movies as $movie): ?>
<tr>
<td><?php echo $movie['Movie']['title']; ?> </td>
<td><?php echo $movie['Movie']['genre']; ?> </td>
<td><?php echo $movie['Movie']['rating']; ?> </td>
<td><?php echo $movie['Movie']['format']; ?> </td>
<td><?php echo $movie['Movie']['length']; ?> </td>
<td class="actions">
<?php echo $this->Html->link('View',
array('action' => 'view', $movie['Movie']['id'])); ?>
<?php echo $this->Html->link('Edit',
array('action' => 'edit', $movie['Movie']['id'])); ?>
<?php echo $this->Html->link('Delete',
array('action' => 'delete', $movie['Movie']['id']),
null, sprintf('Are you sure you want to delete %s?', $movie['Movie']['title'])); ?>
</td>
</tr>
<?php endforeach; ?>
</table>
</div>
<div class="actions">
<h3>Actions</h3>
<ul>
<li><?php echo $this->Html->link('New Movie', array('action' => 'add')); ?></li>
</ul>
</div>
<div class="movies form">
<?php
echo $this->Form->create('Movie');
echo $this->Form->inputs(array('title', 'genre', 'rating', 'format', 'length'));
echo $this->Form->end('Add');
?>
</div>
<div class="actions">
<h3>Actions</h3>
<ul>
<li><?php echo $this->Html->link('List Movies', array('action' => 'index'));?></li>
</ul>
</div>
8.create file "view.ctp" in path to : " /var/www/cakephp/app/View/Movies/ ", and "add" code :
<div class="movies view">9. create file "edit.ctp" in path to : " /var/www/cakephp/app/View/Movies/ ", and "add" code :
<h2> Movie </h2>
<dl>
<dt>Title</dt>
<dd><?php echo $movie['Movie']['title']; ?></dd>
<dt>Genre</dt>
<dd><?php echo $movie['Movie']['genre']; ?></dd>
<dt>Rating</dt>
<dd><?php echo $movie['Movie']['rating']; ?></dd>
<dt>Format</dt>
<dd><?php echo $movie['Movie']['format']; ?></dd>
<dt>Length</dt>
<dd><?php echo $movie['Movie']['length']; ?></dd>
<dt>Created</dt>
<dd><?php echo $movie['Movie']['created']; ?></dd>
<dt>Modified</dt>
<dd><?php echo $movie['Movie']['modified']; ?></dd>
</dl>
</div>
<div class="actions">
<h3>Actions</h3>
<ul>
<li><?php echo $this->Html->link
('Edit Movie', array('action' => 'edit', $movie['Movie']['id'])); ?> </li>
<li><?php echo $this->Html->link
('Delete Movie', array('action' => 'delete', $movie['Movie']['id']),
null, sprintf('Are you sure you want to delete # %s?', $movie['Movie']['id'])); ?> </li>
<li><?php echo $this->Html->link
('List Movies', array('action' => 'index')); ?> </li>
<li><?php echo $this->Html->link
('New Movie', array('action' => 'add')); ?> </li>
</ul>
</div>
<div class="movies form"><?phpecho $this->Form->create('Movie');echo $this->Form->inputs(array('id', 'title', 'genre', 'rating', 'format', 'length'));echo $this->Form->end('Edit');?></div><div class="actions"> <h3>Actions</h3> <ul> <li><?php echo $this->Html->link('List Movies', array('action' => 'index'));?></li> </ul></div>
10. create file "delete.ctp" in path to : " /var/www/cakephp/app/View/Movies/ ", and "add" code :
<div class="movies form">11. Test : http://localhost/cakephp/movies/index(add,edit,delete,view )
<?php
echo $this->Html->link('Delete', array('action' => 'delete', $movie['Movie']['id']), null,
sprintf('Are you sure you want to delete %s?', $movie['Movie']['title']) );
?>
12. Video demo :
0 nhận xét:
Đăng nhận xét