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/cakeRunning it without arguments prints all available commands grouped by source: the application, the framework, plugins.
Main commands
| Command | What it does |
|---|---|
bin/cake server | Starts a server at http://localhost:8765 |
bin/cake server -p 8080 | The same on another port |
bin/cake routes | Shows all routes |
bin/cake cache clear_all | Clears the cache |
bin/cake migrations migrate | Updates the database structure |
bin/cake bake | Shows what the generator can do |
The --help option gives help for any command:
bin/cake server --helpThe 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 HelloThe command creates src/Controller/HelloController.php and a test file for it, and prints the paths of the created files.
What bake can do
| Command | What it creates |
|---|---|
bake controller Articles | A controller in src/Controller |
bake model Articles | Table and entity classes in src/Model |
bake template Articles | Templates in templates/Articles |
bake migration CreateArticles | A migration in config/Migrations |
bake command Report | A console command in src/Command |
bake all Articles | Model, controller and templates at once |
A whole section with one command
bin/cake bake all ArticlesThe 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.