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à: Chức năng thêm dữ liệuBước 1: Tạo tập tin ArticlesTable.php xem lại bài ở trên Bước 2: Tạo tập tin 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 Form->create($data); echo $this->Form->input('username').' Bước 4: Tạo route trong $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ị dataBướ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
Bước 3: Thêm route $routes->connect('/read', ['controller' => 'Articles', 'action' => 'read']); Chức năng xóapublic 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. Installing CakePHP 4Before 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 ComposerCakePHP uses Composer, a dependency management tool, as the officially supported method for installation. Create a CakePHP Projectcomposer create-project --prefer-dist cakephp/app:~4.0 cms
The first screen after installationNow make database cake_cms and make one table articles. CREATE TABLE articles ( Replace the values in the 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 We’ve attached the Timestamp behavior which will automatically populate the 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.
Now check http://localhost/cms/articles/index. Now create view() action in the ArticlesController.php. public function view($slug = null) Now make the view action template file. Make templates/Articles/view.php.
Let’s add the article. public function add() To show a successful message we are using Flash Component. Add following code to ArticlesController.php. public function initialize(): void Create add() action view in File: templates/Articles/add.php.
Now add simple slug generation. // in src/Model/Table/ArticlesTable.php Now add edit action. public function edit($slug) Create edit() action template.
Update index.php view file.
Add a validation rule to ArticlesTable.php. use Cake\Validation\Validator; Add delete() action. public function delete($slug) Update index.php. |