Movatterモバイル変換


[0]ホーム

URL:


Skip to content
DEV Community
Log in Create account

DEV Community

northgoingzax
northgoingzax

Posted on • Edited on

     

CakePHP 3: Bake by example

This article is aimed at newbies to the CakePHP 3 framework.

I'm a fan of CakePHP, and was completely new to it about 9 months ago. Any framework takes some getting used to, but my biggest issue with Cake 3 is the manual. I feel it is written very much with a feeling of "you're familiar with Cake 2, so we don't need explain as much with this one".

Once you get used to using the framework however, it is a very rewarding framework, and luckily theIRC channel andcakephp-3.x tag on stack overflow is actively maintained, so you can work it out or get an answer pretty quickly.

Rather than moan about the manual, I thought I would post an example-based guide to the code generation feature, to help any other new-to-cake devs out there.

These examples are deliberately simple, they're just to get you started with the code generator, rather than just the basic list of commands currently available in thedocs.

Before you use the bake command

  1. Create some tables, following CakePHPconventions
  2. Check you are in the root directory of your project, if you do a directory listingls you should see folders "bin", "config", "src", "webroot"

Create a controller for a table calledusers

bin/cake bake controller Users
Enter fullscreen modeExit fullscreen mode

creates src/Controller/UsersController.php

Create a model (and entity) for a table calledusers

bin/cake bake model Users
Enter fullscreen modeExit fullscreen mode

creates src/Model/Table/UsersTable.php
creates src/Model/Entities/User.php

Create the default template files for a table (add/edit/view/index)

bin/cake bake template Users
Enter fullscreen modeExit fullscreen mode

creates src/Template/Users/index.ctp add.ctp edit.ctp view.ctp

Bake all of the above in 1 command

bin/cake bake all Users
Enter fullscreen modeExit fullscreen mode

Bake just the index template file

bin/cake bake template Users index
Enter fullscreen modeExit fullscreen mode

creates src/Template/Users/index.ctp

Baking for prefix folders (e.g. admin)

If you are baking for an admin section of your site, you will be using separate controller and template files

bin/cake bake controller Users--prefix admin
Enter fullscreen modeExit fullscreen mode

creates src/Controller/Admin/UsersController.php

bin/cake bake template Users--prefix admin
Enter fullscreen modeExit fullscreen mode

creates src/Template/Admin/Users/index.ctp edit.ctp add.ctp view.ctp


Bake from a different database

The previous examples all use the db connection defined inapp.php as'default'. If you have a legacy database for handling client records, e.g.db_records, yourconfig file might look like this

// in config/app.php'Datasources'=>['default'=>['host'=>'localhost','username'=>'db_user','password'=>'pass123','database'=>'db_application',],'records'=>['host'=>'localhost','username'=>'db_records_user','password'=>'pass123','database'=>'db_records',],]
Enter fullscreen modeExit fullscreen mode

Create a model for a table calleduser_records in the records database

bin/cake bake model UserRecords-c records
Enter fullscreen modeExit fullscreen mode

creates /src/Model/Table/UserRecordsTable.php
creates /src/Model/Entities/UserRecord.php

This will include in yourUserRecordsTable.php file

publicstaticfunctiondefaultConnectionName(){return'records';}
Enter fullscreen modeExit fullscreen mode

Create a model from this database for a table not following cake convention, e.g.tbl_records_user

bin/cake bake model UserRecords-c records--table tbl_records_user
Enter fullscreen modeExit fullscreen mode

creates /src/Model/Table/UserRecordsTable.php
creates /src/Model/Entities/UserRecord.php

This will add thedefaultConnectionName() and also set

$this->setTable('tbl_records_user');
Enter fullscreen modeExit fullscreen mode

There are other options you can set here, but some are easier to set by editing the file afterwards. But for example, you can also set thedisplay field to be something other than the default, e.g. email

bin/cake bake model UserRecords-c records--table tbl_records_user--display-field email
Enter fullscreen modeExit fullscreen mode

This will modify the display field

$this->setDisplayField('email');
Enter fullscreen modeExit fullscreen mode

Associations

By default thebake command will look for associations. If you are using a legacy table, or a different datasource, any field headings that end in_id might cause a 'base table or view not found' error.

To avoid this you can bake without associations

bin/cake bake model UserRecords-c records--table tbl_records_user--no-associations
Enter fullscreen modeExit fullscreen mode

Baking with a plugin

Assume you have installed a plugin such asfriendsofcake/BootstrapUI so that your template files are using Bootstrap styles by default
You can now add-t BootstrapUI to any of the above commands

Create the template files using the plugin

creates /src/Template/Users/index.ctp add.ctp edit.ctp view.ctp

bin/cake bake template Users-t BootstrapUI
Enter fullscreen modeExit fullscreen mode

Create the whole MVC skeleton (controller, table, entity, templates) using the legacy database

bin/cake bake all UserRecords-c records--table tbl_records_user-t BootstrapUI
Enter fullscreen modeExit fullscreen mode

Useful Features

Bake relies on the database connection, so to save you loading up and remembering all the tables in the system, you can call a command without specifying the table name to see a list of available tables. Let's say your database has 3 tables:
users
articles
activity_logs

Using the commandbin/cake bake model will produce a list of available tables

bin/cake bake modelChoose a model to bake from the following:UsersArticlesActivityLogs
Enter fullscreen modeExit fullscreen mode

The same goes for:

bin/cake bake controllerbin/cake bake template
Enter fullscreen modeExit fullscreen mode

Hopefully if you're using CakePHP for the first time this is a useful reference for the bake console.

Top comments(6)

Subscribe
pic
Create template

Templates let you quickly answer FAQs or store snippets for re-use.

Dismiss
CollapseExpand
 
beginnerdeveloper profile image
Beginner Developer
Hello! I am Muhammad Ejaaz Razzak Khan, a Software Developer.
  • Location
    Mumbai
  • Education
    T.Y B.S.c IT
  • Work
    Software Developer at Dawat-e-Islami India
  • Joined

When I try to make a model using this commandbin/cake bake model Users

error 👇
bin/cake: No such file or directory

CollapseExpand
 
ibrayandiyev profile image
Ibragim Yandiyev
  • Joined
• Edited on• Edited

You should try bin\cake instead of the bin/cake on Windows OS. Or cd bin&&cake...

CollapseExpand
 
northgoingzax profile image
northgoingzax
LAMP developer, one of many.
  • Location
    Europe
  • Joined

Are you in the project root folder? Have you run composer install?

CollapseExpand
 
beginnerdeveloper profile image
Beginner Developer
Hello! I am Muhammad Ejaaz Razzak Khan, a Software Developer.
  • Location
    Mumbai
  • Education
    T.Y B.S.c IT
  • Work
    Software Developer at Dawat-e-Islami India
  • Joined

Yes

CollapseExpand
 
msamgan profile image
Mohammed Samgan Khan
Love to code with a DRY KISS

nice one bro...

CollapseExpand
 
jovialcore profile image
Chidiebere Chukwudi
Software Developer. php| laravel | cakephp | vuejs/Nuxt | wordpress | Bootstrap | Tailwind

Thank you so much for sharing

Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment'spermalink.

For further actions, you may consider blocking this person and/orreporting abuse

LAMP developer, one of many.
  • Location
    Europe
  • Joined

Trending onDEV CommunityHot

DEV Community

We're a place where coders share, stay up-to-date and grow their careers.

Log in Create account

[8]ページ先頭

©2009-2025 Movatter.jp