1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

cakephp3 命名規則

Last updated at Posted at 2021-01-27

cakephp3 命名規則

規約リンク

データベース

命名は複数形にする
データベースのテーブルの名前(単語単数か複数形か)によって以下で記述のコントローラー、ビュー、モデルの命名も変わる。

例)テーブル名(その場合のコントローラー名)

単数:tests(TestsController.php)
複数:test_ones(TestOnesController.php)
複数:test_two_threes(TestTwoThrees.php)

上記の規約の為、テーブル名を作成するときは単数もしくは複数単語使用する場合「_」で繋げる必要がある。

コントローラー

コントローラーのクラス名は複数形パスカルケースで、最後にControllerが付く
TestsController.php(複数形)

アクションのURL

TestsControllerのviewアクションの場合
tests/view
UsersControllerのindexアクションの場合
users/index

ビュー

テーブル名複数形の名称のフォルダをTemplateフォルダの直下に作成し、その中にコントローラーで記述したアクションに必要なテンプレートファイルを作成していく。
アクション全てに対しテンプレートファイルが必要ではなく、描画する必要のあるアクションに対して作成する。
Template/Tests/view.ctp
Template/Tests/index.ctp
など
ビューのテンプレートファイルは、それを表示するコントローラーの関数に合わせた、 アンダースコア記法で命名されるのでコントローラー側の関数名が
viewAll()のように複数単語のキャメルケースであれば
view_all.ctpとなる。

モデル

テーブル
TestsTable.php(複数形)

エンティティー
Test.php(単数形)

まとめ

各部分を CakePHP の規約に合わせて命名しておくことで、混乱を招く面倒な設定をしなくても 機能的に動作するようになる。以下が最後の規約に合った命名の例

データベースのテーブル: "articles"

Table クラス: ArticlesTable の場所は src/Model/Table/ArticlesTable.php

Entity クラス: Article の場所は src/Model/Entity/Article.php

Controller クラス: ArticlesController は src/Controller/ArticlesController.php

ビューテンプレートの場所は src/Template/Articles/index.ctp

これらの規約により、CakePHP は、 example.com/articles へのリクエストを、 ArticlesController の index() 関数にマップする。

1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?