Catalog / CakePHP Cheat Sheet

CakePHP Cheat Sheet

A comprehensive cheat sheet for CakePHP, covering conventions, core classes, ORM, and common tasks.

CakePHP Conventions

Naming Conventions

Controllers:

Plural, PascalCase, ends with Controller (e.g., ArticlesController)

Models:

Singular, PascalCase (e.g., Article)

Tables:

Plural, PascalCase, ends with Table (e.g., ArticlesTable)

Views:

Lowercase, underscored (e.g., edit.php, view.php)

Database Tables:

Plural, underscored (e.g., articles, users)

Fields in Database:

Lowercase, underscored (e.g., article_id, created_at)

Directory Structure

src/Controller/ - Controller files.
src/Model/Table/ - Table classes (database access).
src/Model/Entity/ - Entity classes (data objects).
templates/ - View files.
config/ - Configuration files.
webroot/ - Publicly accessible assets (CSS, JS, images).

Core Classes & Usage

Controller

$this->loadModel('ModelName');

Loads a model into the controller.

$this->request->getData();

Accesses POST data.

$this->request->getQuery();

Accesses GET parameters.

$this->redirect(['controller' => '...', 'action' => '...']);

Redirects to another action.

$this->set(compact('variable1', 'variable2'));

Sets variables to be available in the view.

$this->Flash->success('Message');

Displays a success flash message.

Table

$this->find('all');

Retrieves all records.

$this->get($id);

Retrieves a single record by ID.

$entity = $this->newEntity($data);

Creates a new entity from data.

$this->patchEntity($entity, $data);

Patches an entity with new data.

$this->save($entity);

Saves an entity to the database.

$this->delete($entity);

Deletes an entity from the database.

Entity

Entities represent individual table rows. You can access properties directly (e.g., $article->title). Use _accessible property to define which fields are mass assignable.

ORM Features

Associations

$this->belongsTo('Authors');

Defines a belongs-to association.

$this->hasMany('Comments');

Defines a has-many association.

$this->belongsToMany('Tags');

Defines a belongs-to-many association.

$this->hasOne('Profile');

Defines a has-one association.

Eager loading associations

$this->Articles->find()->contain(['Authors', 'Comments']);

Retrieving Data

$this->find('all')->where(['Articles.published' => true]);

Find all published articles.

$this->find('list');

Retrieves data as a key-value list.

$this->find('threaded');

Retrieves data as a threaded list.

$this->find('first');

Retrieves first element.

Query Builder

CakePHP provides a powerful query builder for constructing complex database queries. Use the ->select(), ->where(), ->order(), ->limit(), and ->join() methods to customize your queries.

Common Tasks

Generating URLs

$this->Url->build(['controller' => 'Articles', 'action' => 'view', 1]);

Generates a URL to the view action of the Articles controller with ID 1.

Router::url(['controller' => 'Articles', 'action' => 'index']);

Generates a URL using the Router class (outside of views).

$this->Url->asset('img/logo.png');

Generate path to asset.

Form Helper

echo $this->Form->create($article);

Creates a form for the $article entity.

echo $this->Form->input('title');

Creates an input field for the title property.

echo $this->Form->button('Submit');

Creates a submit button.

echo $this->Form->end();

Closes the form.

Baking

CakePHP’s baking tool can generate code for models, views, and controllers. Use the bin/cake bake command to generate code quickly. For example, bin/cake bake controller Articles.