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:
cd c:/xampp/htdocs/myCake4
Execute this command to generate the CRUD based on the configured database table (eg: users table):
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 :)