Naming conventions
A convention is a naming rule. CakePHP uses the controller name to find a table and a template, and a column name to find a link between tables. While names follow the rules, nothing has to be configured. This is the first thing a beginner should learn.
One complete example
Take blog articles. Everything related to them is named like this:
| What | Rule | Name |
|---|---|---|
| Database table | Plural, lower case | articles |
| Table class | Table name plus Table | ArticlesTable |
| Entity class | Singular | Article |
| Controller | Plural plus Controller | ArticlesController |
| Template folder | Controller name without Controller | templates/Articles/ |
| Address | Controller name in lower case | /articles |
Where the files are
src/Controller/ArticlesController.php
src/Model/Table/ArticlesTable.php
src/Model/Entity/Article.php
templates/Articles/index.php
templates/Articles/view.phpA file name always matches the class name. A template is named after the controller method: index.php for index(), view.php for view().
Names of several words
In the database words are separated by underscores, in classes every word starts with a capital letter, in addresses words are joined by hyphens:
| What | Name |
|---|---|
| Table | article_comments |
| Table class | ArticleCommentsTable |
| Entity class | ArticleComment |
| Controller | ArticleCommentsController |
| Address | /article-comments |
Template for the viewAll() method | templates/ArticleComments/view_all.php |
Table columns
| Column | Meaning |
|---|---|
id | Primary key, the record number |
created | Creation date, filled in automatically |
modified | Modification date, filled in automatically |
user_id | A reference to a record of the users table |
A foreign key is a column that stores the number of a record in another table. It is named after that table in the singular with the _id ending. A table that joins two others is named after both in alphabetical order: articles_tags.
How it looks in code
// src/Controller/ArticlesController.php
namespace App\Controller;
class ArticlesController extends AppController
{
public function index(): void
{
$articles = $this->Articles->find()->all();
$this->set('articles', $articles);
}
}There is no table name and no template path here. From the name ArticlesController the framework knows that $this->Articles is the articles table and that templates/Articles/index.php must be rendered.
When a name cannot be changed
An existing database with a blog_posts table is connected with a setting in the table class:
// src/Model/Table/ArticlesTable.php
public function initialize(array $config): void
{
$this->setTable('blog_posts');
}Conventions can be bypassed, but every exception costs an extra line of code.
Common mistake
A table called article instead of articles, or a controller called ArticleController, and CakePHP reports that the table does not exist or the template is missing. The error looks mysterious although the cause is a single letter. Before searching the code, compare your names with the tables in this lesson.