Xây dựng CRUD đầy đủ
Chức năng CRUD là một phần cốt lõi trong mọi hệ thống quản trị nội dung. Trong bài này, chúng ta sẽ thực hiện toàn bộ quy trình thêm mới, hiển thị danh sách, sửa đổi, và xóa bài viết, từ backend (Admin module) của hệ thống Blog.

1. Controller: Admin\PostController
namespace App\Controllers\Admin;
use App\Models\Post;
class PostController extends \Phalcon\Mvc\Controller
{
public function indexAction()
{
$this->view->posts = Post::find();
}
public function createAction()
{
if ($this->request->isPost()) {
$post = new Post();
$post->assign($this->request->getPost());
if ($post->save()) {
$this->flashSession->success("Tạo bài viết thành công");
return $this->response->redirect('/admin/post');
}
$this->flashSession->error("Có lỗi xảy ra");
}
}
public function editAction($id)
{
$post = Post::findFirstById($id);
if (!$post) {
$this->flashSession->error("Không tìm thấy bài viết");
return $this->response->redirect('/admin/post');
}
if ($this->request->isPost()) {
$post->assign($this->request->getPost());
if ($post->save()) {
$this->flashSession->success("Cập nhật thành công");
return $this->response->redirect('/admin/post');
}
$this->flashSession->error("Cập nhật thất bại");
}
$this->view->post = $post;
}
public function deleteAction($id)
{
$post = Post::findFirstById($id);
if ($post && $post->delete()) {
$this->flashSession->success("Xóa bài viết thành công");
} else {
$this->flashSession->error("Không thể xóa bài viết");
}
return $this->response->redirect('/admin/post');
}
}
2. Model: Post.php
namespace App\Models;
use Phalcon\Mvc\Model;
use Phalcon\Validation;
use Phalcon\Validation\Validator\PresenceOf;
class Post extends Model
{
public $id;
public $title;
public $content;
public $created_at;
public function initialize()
{
$this->setSource('posts');
}
public function validation()
{
$validator = new Validation();
$validator->add('title', new PresenceOf([
'message' => 'Tiêu đề không được để trống'
]));
$validator->add('content', new PresenceOf([
'message' => 'Nội dung không được để trống'
]));
return $this->validate($validator);
}
}
3. View
a. index.phtml
<h2>Danh sách bài viết</h2>
<a href="/admin/post/create">+ Thêm mới</a>
<table>
<tr><th>ID</th><th>Tiêu đề</th><th>Hành động</th></tr>
<?php foreach ($posts as $post): ?>
<tr>
<td><?= $post->id ?></td>
<td><?= $post->title ?></td>
<td>
<a href="/admin/post/edit/<?= $post->id ?>">Sửa</a> |
<a href="/admin/post/delete/<?= $post->id ?>">Xóa</a>
</td>
</tr>
<?php endforeach; ?>
</table>
b. create.phtml
& edit.phtml
<form method="post">
<label>Tiêu đề</label><br>
<input type="text" name="title" value="<?= isset($post) ? $post->title : '' ?>"><br>
<label>Nội dung</label><br>
<textarea name="content"><?= isset($post) ? $post->content : '' ?></textarea><br>
<button type="submit">Lưu</button>
</form>
4. Flash message
Đặt trong layout để hiển thị:
<?= $this->flashSession->output() ?>
Kết luận
Chức năng CRUD là một trong những thành phần nền tảng trong mọi ứng dụng quản trị. Bằng việc tổ chức controller hợp lý, sử dụng validation và tương tác qua form, bạn đã hoàn thiện một vòng xử lý dữ liệu cơ bản với Phalcon MVC.
Bài tập thực hành
Yêu cầu:
- Tạo
PostController
cho admin với 4 action:index
,create
,edit
,delete
- Tạo model
Post
có validatetitle
vàcontent
- Tạo giao diện CRUD đơn giản
- Thử thêm, sửa và xóa một bài viết
Gợi ý nâng cao:
- Phân trang danh sách bài viết
- Tích hợp TinyMCE cho field nội dung
- Hiển thị ngày tạo theo định dạng
dd/mm/yyyy

Với hơn 10 năm kinh nghiệm lập trình web và từng làm việc với nhiều framework, ngôn ngữ như PHP, JavaScript, React, jQuery, CSS, HTML, CakePHP, Laravel..., tôi hy vọng những kiến thức được chia sẻ tại đây sẽ hữu ích và thiết thực cho các bạn.
Xem thêm

Chào, tôi là Vũ. Đây là blog hướng dẫn lập trình của tôi.
Liên hệ công việc qua email dưới đây.
lhvuctu@gmail.com