How to generate a full working CRUD skeleton using CakePHP 2.x Bake console?

How to generate a full working CRUD skeleton using CakePHP 2.x Bake console?

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.

Configurations

       Windows

  1. Set the environment variable in your windows machine. Add this line on your environment path –“;D:\xampp\php\;D:\xampp\htdocs\myproject\app\Console\;”
  2. 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.

        Linux

  • No configurations needed, just go to the path – myproject/app/Console in your command line.

CRUD-grid-table-cakePHP

Commands

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)

Functionality

Create

  • In the add.ctp file you can find the form and the fields to add a new record.

Read

  • The listing(index.ctp) comes with sorting and pagination.
    The view.ctp is the view details of the record.

Update

  • It is the edit.ctp, file with pre-filled data in the form to edit the record.

 Delete

  • 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.

Tags:
,
Jay
Jayadev Das
jayadev.das@andolasoft.com

Do what you do best in – that’s what I’ve always believed in and that’s what I preach. Over the past 25+ years (yup that’s my expertise ‘n’ experience in the Information Technology domain), I’ve been consulting to small, medium and large companies ‘bout Web Technologies, Mobile Future as well as on the good-and-bad of tech. Blogger, International Business Advisor, Web Technology Expert, Sales Guru, Startup Mentor, Insurance Sales Portal Expert & a Tennis Player. And top of all – a complete family man!