Encodera

Thanks for reading!

Donate to the site — we work hard for you.

Contents

The console and bake

bin/cake is the console program of the project. It runs the development server, shows routes, clears the cache and creates file skeletons. Creating files is the job of its part called bake.

Where it is

The bin/cake file is inside the project, so commands are run from the project root. In the Windows command prompt write bin\cake instead of bin/cake.

List of commands

bin/cake

Running it without arguments prints all available commands grouped by source: the application, the framework, plugins.

Main commands

CommandWhat it does
bin/cake serverStarts a server at http://localhost:8765
bin/cake server -p 8080The same on another port
bin/cake routesShows all routes
bin/cake cache clear_allClears the cache
bin/cake migrations migrateUpdates the database structure
bin/cake bakeShows what the generator can do

The --help option gives help for any command:

bin/cake server --help

The command prints its description and the list of options.

The bake generator

bake creates a file of the required class in the right folder and with the right name, so conventions are followed automatically:

bin/cake bake controller Hello

The command creates src/Controller/HelloController.php and a test file for it, and prints the paths of the created files.

What bake can do

CommandWhat it creates
bake controller ArticlesA controller in src/Controller
bake model ArticlesTable and entity classes in src/Model
bake template ArticlesTemplates in templates/Articles
bake migration CreateArticlesA migration in config/Migrations
bake command ReportA console command in src/Command
bake all ArticlesModel, controller and templates at once

A whole section with one command

bin/cake bake all Articles

The command reads the columns of the articles table from the database and creates a working section: the list of articles, viewing, adding, editing and deleting. That is why it needs an existing table, which appears in the database chapter. Until then files are created by hand: this way it is clearer what they consist of.

Common mistake

If a file already exists, bake asks whether to overwrite it. Answering y erases everything you wrote in that file by hand. The generator is a way to start quickly, not to update finished code: answer n to the overwrite question when the file contains your changes.

Thanks for reading!

Donate to the site — we work hard for you.