Cakephp Blog Tutorial Part 2 - Naming Conventions

Cakephp Blog Tutorial Part 2 - Naming Conventions



Hi, welcome to the cakephp tutorial part 2 - naming conventions in creating a Cakephp blog. You should already have Cakephp installed and connected to your database. If your main page for your Cakephp application is all green you are ready to go. You should also have a "posts" table in your database. This "posts" table will hold all of our posts for the blog. If you don't have all of this set up please go back and read the Cakephp Blog Tutorial Part 1 - Installation tutorial.

You can also watch this tutorial on youtube.

This tutorial will cover naming conventions for filenames and classnames. We won't actually be interacting with a database in this tutorial. It will be just so you can understand how to name your files and how to access your site through the browser in the URL.

We'll start with model naming conventions. Model file names are the singular version of your table name. We have a "posts" table name. So in this case we want to make a "post" model. So create the following file under app/models:

app/models/
post.php

All model files in Cakephp are classes. The classname is named directly after the filename and if its more than one word it uses pascal case. All model's also extend the AppModel class. The AppModel class gives your models all of Cakephp's model functionality. You also may want to set a "$name" variable and set it equal to "Post" which is the name of the class. This is done for PHP4 compatibility. So lets create our first model like so:

app/models/post.php
                                class Post extends AppModel {
                                  var $name = 'Post';
                                }
                        

Next, lets create our controller. Controller filenames are named right after the name of the tablename except that you append an _controller to the end of the filename. In our case we have a "posts" table, so we want to create a new file for our controller in our app/controllers directory named like so:

app/controllers/
posts_controller.php

Lets open this posts_controller.php file in our text editor. Controllers are also classes. The name of the class is the same as the name of the file using pascal case rather than an underscore. You must also extend this class with the AppController class to give it Cake's controller functionality. You also may want to set a "$name" variable and set it equal to the name of the file, minus the controller word for PHP4 compatibility. So lets edit our posts_controller.php file like so:

app/controllers/posts_controller.php
                                class PostsController extends AppController {
                                  var $name = 'Posts';
                                }
                        

Next, lets create our first action. You can think of actions as an individual page that someone views on your website. You create an action by creating a method/function inside your controllers class. Lets create a simple hello_world action in our posts controller:

app/controllers/posts_controller.php
                                class PostsController extends AppController {
                                  var $name = 'Posts';
                                  
                                  function hello_world() {
                                    
                                  }
                                }
                        

We now need to create a view for our action. All views are stored inside the app/views directory and then inside another folder named after the controller. Since we have a posts controller, inside of our views directroy we need to create a new folder called posts. This posts folder will hold all of the views that correspond to our posts controller's actions. So create the following folder in your views directory:

app/views/
app/views/posts/

Each action inside of your controller will have its own view file. The name of the view is named directly after the action name. So inside of app/views/posts we'll create a new file called hello_world.ctp. View files do not use a .php extension, instead they use a .ctp extension for cake template page I believe. So create the following view file:

app/views/posts/
hello_world.ctp

Now inside of our view file we can use any HTML, CSS, Javascript, or PHP that you'd like. In this view file we're just going to use some simple HTML to output a hello world message. Edit your hello_world.ctp view file like so:

app/views/posts/hello_world.ctp
                                <h2>Hello World</h2>
                                
                                <p>Saying Hello World from our hello_world actions view file.</p>
                        

Now if you go to your browser we can access our posts controller's hello_world action's view. There's 3 segments to a Cakephp URL. The first segment is the cakephp installation, in our case its "tut_blog". The second segment is the name of the controller, in our case its "posts", and the third segment is the action name, in our case its "hello_world". The entire URL to access our hello_world view is:

browser
http://localhost/tut_blog/posts/hello_world

If you hit enter after entering in that URL you should see the HTML we created in our hello_world.ctp view file.

That's pretty much it for the essential Cakephp naming conventions. To wrap it up I'll create a brief table summary of the naming conventions below:

Type tablename Filename Classname Directory
Model posts post.php Post extends AppModel app/models/
Controller posts posts_controller.php PostsController extends AppController app/controllers/
View posts {named after action name}.ctp N/A app/views/{controllerName}/



Return from Cakephp Blog Tutorial Part 2 - Naming Conventions back to Cakephp Resources Section.

Previous: Cakephp Blog Tutorial Part 1 - Installation.