Hướng dẫn crud cakephp 4

Chức năng đọc, thêm, sửa, xóa (CRUD) trong Cakephp 4 rất cần thiết khi quản trị. Nhờ đó mà Admin có thể thêm hay thay đổi bất kỳ dữ liệu nào trên Database mà không cần vào Phpadmin. Vậy để tạo những chức năng này thì sẽ phải làm những gì? Hãy đọc bài này ngay sau đây.

Trước khi thực hành bạn cần tham khảo 2 bài viết đó là:

  • Tạo Controller trong Cakephp 4
  • Model trong Cakephp 4

  • Chức năng thêm dữ liệu
  • Chức năng hiển thị data
  • Chức năng xóa

Chức năng thêm dữ liệu

Bước 1: Tạo tập tin ArticlesTable.php xem lại bài ở trên

Bước 2: Tạo tập tin ArticlesController.php trong Controller

public function add()
    {
        $data = $this->Articles->newEmptyEntity();

        if ($this->request->is('post')) {
            
            $data = $this->Articles->patchEntity($data, $this->request->getData());

            if ($this->Articles->save($data)) {
                $this->Flash->success(__('Dữ liệu đã được lưu vào Database.'));
                return $this->redirect(['action' => 'add']);
            }
            $this->Flash->error(__('Không thể thêm dữ liệu.'));
        }
        $this->set('data', $data);
    }

Bước 3: Tạo file add.php trong template

Form->create($data);
    echo $this->Form->input('username').'
'; echo $this->Form->input('password').'
'; echo $this->Form->button(__('Save')); echo $this->Form->end(); ?>

Bước 4: Tạo route trong route.php

$routes->connect('/add', ['controller' => 'Articles', 'action' => 'add']);

Cuối cùng truy cập vào http://localhost/cakephp/add để thêm Data.

Chức năng hiển thị data

Bước 1: Thêm function trong Controller

public function read()
    {
        $data = $this->Articles->find('all');
        $this->set(compact('data'));
    }

Bước 2: Thêm tập tin read.php trong templates

";
      echo "";
      echo "";
      echo "";
      echo "";
endforeach;
?>
".$row['id']."".$row['username']."".$row['password']."EditDelete

Bước 3: Thêm route

$routes->connect('/read', ['controller' => 'Articles', 'action' => 'read']);

Chức năng xóa

public function delete($id){
         $data = TableRegistry::get('Articles');
         $users = $data->get($id);
         $data->delete($users);
         echo "Xóa thành công!";
         $this->setAction('read');
    }

Thêm route

$routes->connect('/delete/{$id}', ['controller' => 'Articles', 'action' => 'delete']);

Đang cập nhật thêm…

Dữ liệu trong cơ sở dữ liệu sau khi có CRUD thì sẽ rất tiện lợi. Quản trị viên sẽ dễ dàng quản lý thông tin và sửa xóa nhanh chóng. Với bài viết này bạn sẽ từng bước nắm vững kiến thức về Cakephp 4 rồi đấy.

The CakePHP team is thrilled to announce the immediate availability of 4.0.0.CakePHP 4 is coming with breaking changes. This new release is targeting the newest versions of PHP especially 7.2. CakePHP 4 supports FormHelper now generates HTML5 validation messages and DateTime inputs, Middleware for CSP headers, Form tampering prevention, and HTTPS enforcement.

CakePHP has a few system requirements:

HTTP Server. For example Apache. Having mod_rewrite is preferred, but by no means required. You can also use nginx, or Microsoft IIS if you prefer.
Minimum PHP 7.2
mbstring PHP extension
intl PHP extension
simplexml PHP extension
PDO PHP extension

Installing CakePHP 4

Before starting you should make sure that your PHP version is up to date.You should have PHP 7.2 (CLI) or higher.

php -v

Installing Composer

CakePHP uses Composer, a dependency management tool, as the officially supported method for installation.

Create a CakePHP Project

composer create-project --prefer-dist cakephp/app:~4.0 cms

Hướng dẫn crud cakephp 4

The first screen after installation

Now make database cake_cms and make one table articles.

CREATE TABLE articles (
id INT AUTO_INCREMENT PRIMARY KEY,
user_id INT NOT NULL,
title VARCHAR(255) NOT NULL,
slug VARCHAR(191) NOT NULL,
body TEXT,
published BOOLEAN DEFAULT FALSE,
created DATETIME,
modified DATETIME,
UNIQUE KEY (slug)
) CHARSET=utf8mb4;

Replace the values in the Datasources.default array in your config/app_local.php file.

Now let’s make the heart of CakePHP. They enable us to read and modify our data. They allow us to build relations between our data, validate data, and apply application rules. The file we’ll be creating will be saved to src/Model/Table/ArticlesTable.php.

// src/Model/Table/ArticlesTable.php
namespace App\Model\Table;
use Cake\ORM\Table;class ArticlesTable extends Table
{
public function initialize(array $config): void
{
$this->addBehavior('Timestamp');
}
}

We’ve attached the Timestamp behavior which will automatically populate the created and modified columns of our table. We’ll also create an Entity class for our Articles. Entities represent a single record in the database. Our entity will be saved to src/Model/Entity/Article.php.

Now we need controllers. Controllers in CakePHP handle HTTP requests and execute business logic contained in model methods, to prepare the response. Place ArticlesController.php inside the src/Controller directory.

Now create a listing page. Create templates/Articles/index.php.

Articles












Title Created

Html->link($article->title, ['action' => 'view', $article->slug]) ?>

created->format(DATE_RFC850) ?>

Now check http://localhost/cms/articles/index.

Now create view() action in the ArticlesController.php.

public function view($slug = null)
{
$article = $this->Articles->findBySlug($slug)->firstOrFail();
$this->set(compact('article'));
}

Now make the view action template file. Make templates/Articles/view.php.

title) ?>


body) ?>


Created: created->format(DATE_RFC850) ?>


Html->link('Edit', ['action' => 'edit', $article->slug]) ?>

Let’s add the article.

public function add()
{
$article = $this->Articles->newEmptyEntity();
if ($this->request->is('post')) {
$article = $this->Articles->patchEntity($article, $this->request->getData());

// Hardcoding the user_id is temporary, and will be removed later
// when we build authentication out.
$article->user_id = 1;

if ($this->Articles->save($article)) {
$this->Flash->success(__('Your article has been saved.'));
return $this->redirect(['action' => 'index']);
}
$this->Flash->error(__('Unable to add your article.'));
}
$this->set('article', $article);
}

To show a successful message we are using Flash Component. Add following code to ArticlesController.php.

public function initialize(): void
{
parent::initialize();
$this->loadComponent('Paginator');
$this->loadComponent('Flash'); // Include the FlashComponent
}

Create add() action view in File: templates/Articles/add.php.

Add Article


echo $this->Form->create($article);
// Hard code the user for now.
echo $this->Form->control('user_id', ['type' => 'hidden', 'value' => 1]);
echo $this->Form->control('title');
echo $this->Form->control('body', ['rows' => '3']);
echo $this->Form->button(__('Save Article'));
echo $this->Form->end();
?>

Now add simple slug generation.

// in src/Model/Table/ArticlesTable.php
namespace App\Model\Table;

use Cake\ORM\Table;
// the Text class
use Cake\Utility\Text;
// the EventInterface class
use Cake\Event\EventInterface;

// Add the following method.

public function beforeSave(EventInterface $event, $entity, $options)
{
if ($entity->isNew() && !$entity->slug) {
$sluggedTitle = Text::slug($entity->title);
// trim slug to maximum length defined in schema
$entity->slug = substr($sluggedTitle, 0, 191);
}
}

Now add edit action.

public function edit($slug)
{
$article = $this->Articles
->findBySlug($slug)
->firstOrFail();

if ($this->request->is(['post', 'put'])) {
$this->Articles->patchEntity($article, $this->request->getData());
if ($this->Articles->save($article)) {
$this->Flash->success(__('Your article has been updated.'));
return $this->redirect(['action' => 'index']);
}
$this->Flash->error(__('Unable to update your article.'));
}

$this->set('article', $article);
}

Create edit() action template.

Edit Article


echo $this->Form->create($article);
echo $this->Form->control('user_id', ['type' => 'hidden']);
echo $this->Form->control('title');
echo $this->Form->control('body', ['rows' => '3']);
echo $this->Form->button(__('Save Article'));
echo $this->Form->end();
?>

Update index.php view file.

Articles


Html->link("Add Article", ['action' => 'add']) ?>













Title Created Action

Html->link($article->title, ['action' => 'view', $article->slug]) ?>

created->format(DATE_RFC850) ?>

Html->link('Edit', ['action' => 'edit', $article->slug]) ?>

Add a validation rule to ArticlesTable.php.

use Cake\Validation\Validator;

// Add the following method.
public function validationDefault(Validator $validator): Validator
{
$validator
->notEmptyString('title')
->minLength('title', 10)
->maxLength('title', 255)

->notEmptyString('body')
->minLength('body', 10);

return $validator;
}

Add delete() action.

public function delete($slug)
{
$this->request->allowMethod(['post', 'delete']);
$article = $this->Articles->findBySlug($slug)->firstOrFail();
if ($this->Articles->delete($article)) {
$this->Flash->success(__('The {0} article has been deleted.', $article->title));
return $this->redirect(['action' => 'index']);
}
}

Update index.php.

Articles


Html->link("Add Article", ['action' => 'add']) ?>













Title Created Action

Html->link($article->title, ['action' => 'view', $article->slug]) ?>

created->format(DATE_RFC850) ?>

Html->link('Edit', ['action' => 'edit', $article->slug]) ?>
Form->postLink(
'Delete',
['action' => 'delete', $article->slug],
['confirm' => 'Are you sure?'])
?>