Thứ Ba, 10 tháng 9, 2013

Xây dựng tính năng thêm, sửa, xóa của một album nhạc bằng Zend Framework trên Linux

Posted by Unknown Thứ Ba, tháng 9 10, 2013, under | No comments


Mình viết bài thì rất hạn chế ly thuyết, điễn dải nhiều ,vì mình nghèo văn ,các bạn thông cảm nha !


Các bước thực hiện:

1. Tạo database trong mysql : z-click ( terminal : create database z-click; )

2. Tạo 1 table trong database z-click : albums
CREATE TABLE albums (
id int(11) NOT NULL auto_increment,
artist varchar(100) NOT NULL,
title varchar(100) NOT NULL,
PRIMARY KEY (id)
);

3. Chèn vào table  albums nội dung sau :

INSERT INTO albums (artist, title)
VALUES
('Paolo Nutine', 'Sunny Side Up'),
('Florence + The Machine', 'Lungs'),
('Massive Attack', 'Heligoland'),
('Andre Rieu', 'Forever Vienna'),
('Sade', 'Soldier of Love');

3. Trong terminal ,di chuyển con trỏ đến thư mục cài đặt ZendFramework , nhập vào lần lược các dòng lệnh sau:

zf create action add Index
zf create action edit Index
zf create action delete Index

4. Trong terminal ,di chuyển con trỏ đến thư mục cài đặt ZendFramework , nhập vào  dòng lệnh sau:

zf create db-table Albums albums

5. từ đường dẫn  .. /application/models/DbTable/Albums.php ,mở file Albums.php lên , cho đoạn code sau vào ,rồi save lại


<?php
class Application_Model_DbTable_Albums extends Zend_Db_Table_Abstract
{
protected $_name = 'albums';
public function getAlbum($id)
 {
$id = (int)$id;
$row = $this->fetchRow('id = ' . $id);
if (!$row) {
throw new Exception("Could not find row $id");
 }
return $row->toArray();
 }
public function addAlbum($artist, $title)
 {
$data = array(
'artist' => $artist,
'title' => $title,
 );
$this->insert($data);
 }
public function updateAlbum($id, $artist, $title)
 {
$data = array(
'artist' => $artist,
'title' => $title,
 );
$this->update($data, 'id = '. (int)$id);
 }
public function deleteAlbum($id)
 {
$this->delete('id =' . (int)$id);
 }
}

6. Trong terminal ,di chuyển con trỏ đến thư mục cài đặt ZendFramework , nhập vào  dòng lệnh sau:

zf enable layout

7. từ đường dẫn  .. /application/layouts/scripts/layout.phtml  ,mở file layout.phtml lên , cho đoạn code sau vào ,rồi save lại :

<?php
$this->headMeta()->appendHttpEquiv('Content-Type', 'text/html;charset=utf-8');
$this->headTitle()->setSeparator(' - ');
$this->headTitle('Zend Framework Tutorial');
echo $this->doctype(); ?>
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<?php echo $this->headMeta(); ?>
<?php echo $this->headTitle(); ?>
<?php echo $this->headMeta(); ?>
<?php echo $this->headTitle(); ?>
<?php echo $this->headLink()->prependStylesheet($this->baseUrl().'/css/site.css'); ?>
</head>
<body>
<div id="content">
<h1><?php echo $this->escape($this->title); ?></h1>
<?php echo $this->layout()->content; ?>
</div>
</body>
</html>

8.  Tạo file site.css  theo đường daazn sau : .. /public/css/site.css ( tạo folder css nữa  nha ) ,sau đó lại cho code sau vào , save lại :

body,html {
margin: 0 50px;
font-family: Verdana,sans-serif;
}
h1 {
font-size: 1.4em;
color: #008000;
}
a {
color: #008000;
}
/* Table */
th {
text-align: left;
}
td, th {
padding-right: 5px;
}
/* style form */
form dt {
width: 100px;
display: block;
float: left;
clear: left;
}
form dd {
margin-left: 0;
float: left;
}
form #submitbutton {
margin-left: 100px;
}

9.   từ đường dẫn     .. /application/views/scripts/index/index.phtml ,mở file index.phtml lên , cho đoạn code sau vào ,rồi save lại :

<?php
$this->title = "My Albums";
$this->headTitle($this->title);
?>
<p><a href="<?php echo $this->url(array('controller'=>'index',
'action'=>'add'));?>">Add new album</a></p>
<table>
<tr>
<th>Title</th>
<th>Artist</th>
<th>&nbsp;</th>
</tr>
<?php foreach($this->albums as $album) : ?>
<tr>
<td><?php echo $this->escape($album->title);?></td>
<td><?php echo $this->escape($album->artist);?></td>
<td>
<a href="<?php echo $this->url(array('controller'=>'index',
'action'=>'edit', 'id'=>$album->id));?>">Edit</a>
<a href="<?php echo $this->url(array('controller'=>'index',
'action'=>'delete', 'id'=>$album->id));?>">Delete</a>
</td>
</tr>
<?php endforeach; ?>
</table>


10.  từ đường dẫn     .. /application/forms/Album.php ,mở file Album.php lên , cho đoạn code sau vào ,rồi save lại :


<?php
class Application_Form_Album extends Zend_Form
{
public function init()
{
$this->setName('album');
$id = new Zend_Form_Element_Hidden('id');
 $id->addFilter('Int');
$artist = new Zend_Form_Element_Text('artist');
$artist->setLabel('Artist')
->setRequired(true)
->addFilter('StripTags')
->addFilter('StringTrim')
->addValidator('NotEmpty');
$title = new Zend_Form_Element_Text('title');
$title->setLabel('Title')
->setRequired(true)
->addFilter('StripTags')
->addFilter('StringTrim')
->addValidator('NotEmpty');
$submit = new Zend_Form_Element_Submit('submit');
$submit->setAttrib('id', 'submitbutton');
$this->addElements(array($id, $artist, $title, $submit));
}
}

11.   từ đường dẫn     .. /application/views/scripts/index/add.phtml, mở file add.phtml lên , cho đoạn code sau vào ,rồi save lại :

<?php
$this->title = "Add new album";
$this->headTitle($this->title);
echo $this->form ;
?>

12.    từ đường dẫn     ..  /application/views/scripts/index/edit.phtml, mở file edit.phtml lên , cho đoạn code sau vào ,rồi save lại :

<?php
$this->title = "Edit album";
$this->headTitle($this->title);
echo $this->form ;
?>

13.   từ đường dẫn     ..  /application/views/scripts/index/delete.phtml  , mở file delete.phtml lên , cho đoạn code sau vào ,rồi save lại :

<?php
$this->title = "Delete album";
$this->headTitle($this->title);
?>
<p>Are you sure that you want to delete
 '<?php echo $this->escape($this->album['title']); ?>' by
 '<?php echo $this->escape($this->album['artist']); ?>'?
</p>
<form action="<?php echo $this->url(array('action'=>'delete')); ?>" method="post">
<div>
<input type="hidden" name="id" value="<?php echo $this->album['id']; ?>" />
<input type="submit" name="del" value="Yes" />
<input type="submit" name="del" value="No" />
</div>
</form>

14. từ đường dẫn     .. /application/controllers/IndexController.php  , mở file IndexController.php lên , cho đoạn code sau vào ,rồi save lại :

<?php

class IndexController extends Zend_Controller_Action
{

    public function init()
    {
        /* Initialize action controller here */
    }

    public function indexAction()
    {
        // action body
$albums = new Application_Model_DbTable_Albums();
$this->view->albums = $albums->fetchAll();
    }

    public function aboutAction()
    {
        // action body
    }

    public function vcdAction()
    {
        // action body
    }

    public function showsAction()
    {
        // action body
    }

    public function contactAction()
    {
        // action body
    }

    public function newsAction()
    {
        // action body
    }

    public function addAction()
    {
        // action body

$form = new Application_Form_Album();
$form->submit->setLabel('Add');
$this->view->form = $form;
if ($this->getRequest()->isPost()) {
$formData = $this->getRequest()->getPost();
if ($form->isValid($formData)) {
$artist = $form->getValue('artist');
$title = $form->getValue('title');
$albums = new Application_Model_DbTable_Albums();
$albums->addAlbum($artist, $title);
$this->_helper->redirector('index');
} else {
$form->populate($formData);
}
}

    }

    public function editAction()
    {
$form = new Application_Form_Album();
$form->submit->setLabel('Save');
$this->view->form = $form;
if ($this->getRequest()->isPost()) {
$formData = $this->getRequest()->getPost();
if ($form->isValid($formData)) {
$id = (int)$form->getValue('id');
$artist = $form->getValue('artist');
$title = $form->getValue('title');
$albums = new Application_Model_DbTable_Albums();
$albums->updateAlbum($id, $artist, $title);
$this->_helper->redirector('index');
} else {
$form->populate($formData);
}
} else {
$id = $this->_getParam('id', 0);
if ($id > 0) {
$albums = new Application_Model_DbTable_Albums();
$form->populate($albums->getAlbum($id));
}
}

    }

    public function deleteAction()
    {
if ($this->getRequest()->isPost()) {
$del = $this->getRequest()->getPost('del');
if ($del == 'Yes') {
$id = $this->getRequest()->getPost('id');
$albums = new Application_Model_DbTable_Albums();
$albums->deleteAlbum($id);
}
$this->_helper->redirector('index');
} else {
$id = $this->_getParam('id', 0);
$albums = new Application_Model_DbTable_Albums();
$this->view->album = $albums->getAlbum($id);
}
    }


}


14.  Sửa lại file application như sau :

[production]
phpSettings.display_startup_errors = 0
phpSettings.display_errors = 0
includePaths.library = APPLICATION_PATH "/../library"
bootstrap.path = APPLICATION_PATH "/Bootstrap.php"
bootstrap.class = "Bootstrap"
appnamespace = "Application"
resources.frontController.controllerDirectory = APPLICATION_PATH "/controllers"
resources.frontController.params.displayExceptions = 0
resources.layout.layoutPath = APPLICATION_PATH "/layouts/scripts/"
resources.view[] =
resources.view.doctype = "XHTML1_STRICT"

// phan nay dung ket noi csdl, ban nen thay doi cho phu hop nha

resources.db.adapter = "Pdo_Mysql"
resources.db.params.charset = "utf8"
resources.db.params.host = "localhost"
resources.db.params.username = "root"
resources.db.params.password = "huy"
resources.db.params.dbname = "z_click"

//

[staging : production]

[testing : production]
phpSettings.display_startup_errors = 1
phpSettings.display_errors = 1

[development : production]
phpSettings.display_startup_errors = 1
phpSettings.display_errors = 1
resources.frontController.params.displayExceptions = 1

15. Sửa lại file Bootstap.php như sau :
<?php

class Bootstrap extends Zend_Application_Bootstrap_Bootstrap
{

protected function _initDoctype()
{
      $this->bootstrap('view');
      $view = $this->getResource('view');
$view->doctype('XHTML1_STRICT');

}
}

16. Tài lệu : https://docs.google.com/file/d/0B6JhErFMAx7saE5raFBycWdodzQ/edit

17. Video demo : http://www.youtube.com/watch?v=tV1Pw-cJ6VM&feature=youtu.be




Xem Nhiều

Bài đăng phổ biến

Lưu trữ blog

Blog Archive