CakePHP Naming Conventions
CakePHP requires us to follow certain convention. In this way, we will have very uniform system development, have free functionality, and save time over tracking configuration files! :)1. Create a Model
- directory: app/models/
- filename: post.php
- classname: Post
- extension: class Post extends AppModel
The filenames of models are always singular. And its classnames are CamelCased.
2. Create a Controller
- directory: app/controllers/
- filename: posts_controller.php
- classname: PostsController
- extension: class PostsController extends AppController
The filename of a controller is always plural followed by an underscore then the word "controller" (_controller). Classnames are also CamelCased.
3. Creat a View
- directory: app/views/posts/
- filename: hello_world.ctp
You will create another view directory for each of your models. In this case, we created a directory called "posts" under app/views/ directory. Filenames are saved with the extension .ctp (I believe its "cake template page"). They are also named after the method/function (action) inside your controller.
If you have more than one word object (ex. Sponsored Member), you may use underscores to name it. In my case, I will name it "sponsored_member".
Model: sponsored_member.php
View: app/views/sponsored_members
Controller: sponsored_members_controller.php
Hi! I'm Mike Dalisay, the co-founder of codeofaninja.com, a site that helps you build web applications with PHP and JavaScript. Need support? Comment below or contact [email protected]
I'm also passionate about technology and enjoy sharing my experience and learnings online. Connect with me on LinkedIn, Twitter, Facebook, and Instagram.