Skip to content
CODE THE PIXEL
On this page
CakePHP

CakePHP 4 CRUD

Asyraf Wahi Anuar Updated 2 min read

CRUD operation in CakePHP enables the developer to rapidly generate the prototype for a web application. CRUD comprise routine code for create, read, update and delete operation.

This tutorial may be outdated

It was last reviewed 4 years ago. Some steps may differ in newer versions.

Tech stack

  • CakePHP 4.x

CRUD creation using console commands is one of my favourite features in CakePHP. This tutorial will guide you on how to generate CRUD based on the database using console commands. Console applications are ideal for handling a variety of background & maintenance tasks that leverage your existing application configuration, models, plugins and domain logic.

Database configuration

The database should be connected with the CakePHP framework. Refer here to configure the database connection.

Execute the Command Prompt/ Windows Power Shell/ Git Bash and navigate the target directory to your development folder eg: myCake4 folder, using the change directory command:

Terminal
cd c:/xampp/htdocs/myCake4

Execute this command to generate the CRUD based on the configured database table (eg: users table):

Terminal
bin\cake bake all users

It will generate:

  • Model (...src/Model/Entity/User.php & ...src/Model/Entity/UsersTable.php)

  • Controller (.../src/Controller/UsersController.php)

  • View (...templates/Users/[add.php, edit.php, view.php & index.php])

  • Tests file

Now, navigate to localhost/myCake4/users and it should render the index page for the user table as follows:

The add new user page will render as follows:

Repeat for other tables in your database.

That all. Happy coding :)

Did this tutorial help?

213 readers found this helpful

Comments

No comments yet. Questions about this tutorial are welcome here: the answer helps the next reader too.

Leave a comment

Markdown works: `code`, ``` blocks, **bold**, links.

First comments are reviewed before they appear.

CakePHP

Using Bahasa Melayu Table Names in CakePHP

Learn how to use Bahasa Melayu table names like "pengguna" in CakePHP while following the framework’s naming conventions. Includes tips for using the Bake tool and avoiding pluralization issues.

5 min read

Tools & Software

Formatting PHP in VSCode Using Prettier and PHP Intelephense

Learn how to set up Visual Studio Code (VSCode) for automatic PHP file formatting using the Prettier and PHP Intelephense extensions. This step-by-step tutorial will help you maintain a clean and organised codebase, enhancing your PHP development experience.

2 min read