Scaffolding a Blog
Scaffolding is a great way of getting the early parts of developing a web application started. Early database schemas are volatile and subject to change, which is perfectly normal in the early part of the design process. This has a downside: a web developer hates creating forms that never will see real use. To reduce the strain on the developer, scaffolding is now introduced in Cake.
Scaffolding analyzes your database tables and creates standard lists with add, delete and edit buttons, standard forms for editing and standard views for inspecting a single item in the database.
Create the Posts Database Table
Create a database table for posts and seed it with some data.
CREATE TABLE posts ( id INT UNSIGNED AUTO_INCREMENT PRIMARY KEY, title VARCHAR(50), body TEXT, created DATETIME DEFAULT NULL, modified DATETIME DEFAULT NULL ); INSERT INTO posts (title,body,created) VALUES ('The title', 'This is the post body.', NOW()); INSERT INTO posts (title,body,created) VALUES ('A title once again', 'And the post body follows.', NOW()); INSERT INTO posts (title,body,created) VALUES ('Title strikes back', 'This is really exciting! Not.', NOW());
The table name is plural, which is the default for all the tables used by Cake. The id, created and modified fields are special as you will soon find out. For now that means that you should not change their names.
Create the Model Class (the M in MVC)
Models are the part of you app that tells the rest of the application how to handle all the CRUD (Create, Retrieve, Update, and Delete), you did know that CAKE can handle the CRUD
for you.
Now create a model class for the posts table by creating app/models/post.php containing:
app/models/post.php
<?php class Post extends AppModel { var $name = 'Post'; // This is the name of your database table } ?>
This will be enough for Cake to notice the model, load it, and connect it with the database table. Notice, that the model class name is singular. It’s important to stick to this naming convention, because otherwise Cake will not run automatically. By default, the model class and the database table use the same name in singular and plural forms, respectively.
Create the Controller Class (the C in MVC)
Create the controller class. Put the following in app/controllers/posts_controller.php:
app/controllers/posts_controller.php
<?php class PostsController extends AppController { var $name = 'Posts'; // http://localhost/Posts/ or http://localhost/posts/ // Setting $name='Posts' allows you to access your page //If you make changes, verify your cache with a couple of refreshes var $scaffold; // this is where the magic happens } ?>
The controller is ready. Really that’s it!
Create the Views (the V in MVC)
Now comes the fancy step, get ready this is really hard. Create a folder in app/views called posts so you end up with a path like
app/views/posts
This step is not necessary for scaffolding, but you will need this directory as soon as you want to create your own views.
Have a look
Now go to that view in the browser
http://localhost/~username/posts/
Or however you get to where it is on your server/machine. Now you should see the following
Try view
How about edit you say
Overriding Scaffold Views
Basically there are two ways to overide the views that scaffold creates:
1) provide a “scaffold.:action:.thtml” template . This reuses the scaffold controller action code. You only need to provide the app/views/:controller:/scaffold.:action:.thml
For example to override the ‘index’ view, create a file called ‘scaffold.list.thtml’ (or in v0.10: ‘scaffold_index.thtml’) in the /app/views/posts/ folder you made earlier. The fastest way to get this done is to copy the file /cake/libs/view/templates/scaffolds/list.thtml . Next you can start customizing this view, without needing to modify controller code.
If the scaffold controller action is not sufficient then you need to:
2) provide an action and :action:.thtml template
You’ll create a function in the controller for the view you want to override as well as a a file in the view folder for that controller.
For example, to override the ‘index’ view, create a file called ‘index.thtml’ in the /app/views/posts/ folder you made earlier. The fastest way to get started with a new view is to copy the HTML from the view generated when using scaffolding. To do that, view the source for the index page in your browser of choice, copy the code between the opening content div (<div id=”content”>) and the closing content div (</div>). Paste that into your new ‘index.thtml’ file and you’ll have the basics there to work with rather than starting from scratch.
Next, you’ll need to change your PostsController (/app/controllers/posts_controller.php) include the ‘index’ function:
class PostsController extends AppController { var $name = 'Posts'; var $scaffold; // leave this here so Cake can scaffold the other views that are not overridden function index() { } }
Adding the ‘index’ function to the PostsController will keep scaffolding from attempting to do the work for you and will look for an ‘index.thtml’ file instead.
Once you get that page to show up, you can begin making changes to it and replace the generated HTML with some CakePHP (see blog_tutorial_-_1).
Summary
So what have you done
- Created a database table
- Create 2 files ( models/post.php, controllers/posts_controller.php )
- Create a folder ( views/posts/ )
- Opened in browser
Want to do it faster try rdBaker
Check out Bake ways to get you started with your very own cake.
Next Steps
You might want to move on up to creating your own controller and views for that try Blog Tutorial 1
Good luck and enjoy your CAKE.


