How to use CakePHP Bake Features

How to use CakePHP Bake Features

CakePHP Bake is a powerful way to give your project a jump start!  Cake Bake can create models, templates and controllers for you.  You can even customize the templates used, so if you have a specific theme you want to adhere to, you're able to modify the output baked.

Getting Started - How to Use Bake

  1. Open your Windows command prompt
  2. Use cd to navigate to the "\bin\" folder in the root of your CakePHP4 project.  Now you should be in YourCakePHPProject\bin\
  3. Type the following to see if you are in the correct folder - you should see the output below:
cake bake
No command provided. Run `bake --help` to get a list of commands.

Using Bake

Using Bake generates your models, controllers and templates for an existing table.  You will need to have your application setup to access your database, and you will need to have created the table you want to bake against.  Bake creates the files and Cake stuff for CRUD (Create, Update, Delete).  By default the Cake Bake process will check for related keys (for example, a users_options table might have a "user_id" field; Bake will check for a users table and automatically generate CakePHP relationships between users.id and users_options.user_id, for the table you're baking against).  In some cases, you might want to modify those relationsihps and default values.

Use the following commands to create your model, controller and template (view) for a table:

c:\my project\bin>  cake bake model my_table <enter>
c:\my project\bin>  cake bake controller my_table <enter>
c:\my project\bin>  cake bake template my_table <enter>
c:\my project\bin> cake bake all my_table <enter>

After the files are generated, you will find them in the \src\Model, \src\Controller and \templates\MyTable folders.

 

You can also have Cake generate code for seperate prefixes and folders, like Admin by using the --prefix option.

cake bake controller my_table --prefix admin
//note: using --prefix is not available for model

This generates a file in /src/Controller/Admin/MyTableController.php

 

 

 

Share this Post