How to generate a full working CRUD skeleton using CakePHP 2.x Bake console?
January 9, 2015
CakePHP’s bake console is a command line tool to build fully functional CRUD(Create, Read, Update, Delete) skeleton for your project. As we know CakePHP 3.x is at our doorstep and scaffolding has been removed from it. Cake’s bake console is the extensive scaffolding that generates well documented code for the CRUD operation.
The Cake’s bake console helps you to generate the code for model validations and model relationship. It will ask you, what kind of validation you want and also suggest you the model relationship as per the database convention. We just have to confirm them in command prompt. Going further, Bake can also write Unit Tests for you.
- Set the environment variable in your windows machine. Add this line on your environment path –“;D:\xampp\php\;D:\xampp\htdocs\myproject\app\Console\;”
- Open the windows command prompt and go to the path – myproject/app/Console
NOTE: If you you have git bash in windows, you don’t have to set the environment variables. Go to the project folder myproject/app/Console and open git bash.
- No configurations needed, just go to the path – myproject/app/Console in your command line.
The bake console commands are very easy. It is well documented and it will walk you through the process while you are generating the code.
cake bake – is the command to start generating CRUD and it will ask, what do you want to bake.
cake bake all – this will generate code for your model, view and controller. But before that, it will ask you to select a model(as per the database table).
Other commands are: cake bake controller, cake bake model, cake bake view, cake bake project
List of Files (Command: cake bake all)
- It creates your Controller with four actions – index, view, add, edit.
- The same index, view, add, edit .ctp view files will be created respectively in the view folder.
- Model file will be created with all the validations and relationships stated by you while generating them on bake console. (If you have an existing Model, it will ask you to overwrite that)
- In the add.ctp file you can find the form and the fields to add a new record.
- The listing(index.ctp) comes with sorting and pagination.
The view.ctp is the view details of the record.
- It is the edit.ctp, file with pre-filled data in the form to edit the record.
- In the listing, you can find the delete link in each records to delete that record.
People say, Cake’s bake is the best friend of “lazy type” developer, but its not true. Smart developer does things quicker and faster with the help of CakePHP’s bake console.
We hope this article would help you to kickstart your CakePHP project real quick. If you have any question or suggestions, you can ask us in the comment section.