BlogHow to create a blog with theRecess PHP Framework, Part 1This is the first article in my Recess Framework blog tutorial. This tutorial will
introduce you to the Recess Framework, a RESTful and open source PHP
framework sponsored by New Media Campaigns. You will also learn how to create a
fully functional blog with comments, categories, and authors.
I will use Mac OS X and TextMate during this tutorial. However, the Recess
Framework will also work on Windows or Linux. Your system should meet these
minimum system requirements:
Apache web server1.
PHP 5.2.4 or greater2.
SQLite 33.
Download and install the Recess FrameworkDownload Recess Edge1.
Create a web-accessible directory for our blog2.
Unzip the Recess Edge download into the blog directory3.
View the blog directory in a web browser4.
How to create a blog with the Recess PHP Framework, Part 1 http://www.newmediacampaigns.com/page/how-to-create-a-b...
1 of 15 7/10/14 4:22 PM
If you see a Congratulations, Recess is almost setup! message, the Recess
Framework has been installed successfully. If you do not see this message, be sure
you include index.php at the end of the URL in your web browser. If you still do not
see this message, review the installation documentation on the Recess Framework
website. From this point forward, the path to the filesystem directory created for this
blog will be called the [BLOG_ROOT]; the base URL with which you view this blog
in your web browser will be called the [BLOG_URL]. On my machine, the
[BLOG_URL] is http://localhost/~joshlockhart/recess/ and the
[BLOG_ROOT] is ~/Sites/recess/ . These paths will be different if you use
Windows or Linux.
Setup the blog database
NEWMEDIACAMPAIGNS
About Services Work Blog Labs Contact
How to create a blog with the Recess PHP Framework, Part 1 http://www.newmediacampaigns.com/page/how-to-create-a-b...
2 of 15 7/10/14 4:22 PM
For simplicity, our blog will use a SQLite database. The SQLite database will be
created automatically at [BLOG_ROOT]data/sqlite/default.db . First, we need to
make several filesystem directories writeable by the web server. You can make
these directories writeable with the following commands:
$ chmod -R 0777 [BLOG_ROOT]data/temp/1.
$ chmod -R 0777 [BLOG_ROOT]data/sqlite/2.
Next, we need to tell Recess to use a SQLite database. Open [BLOG_ROOT]recess-
conf.php in a text editor. Uncomment the SQLite item in the
RecessConf::$defaultDatabase array. You can delete the MySQL item from this
array. The final array should look like this:
RecessConf::$defaultDatabase = array(
'sqlite:' . $_ENV['dir.bootstrap'] . 'data/sqlite/default.db'
);
NEWMEDIACAMPAIGNS
About Services Work Blog Labs Contact
How to create a blog with the Recess PHP Framework, Part 1 http://www.newmediacampaigns.com/page/how-to-create-a-b...
3 of 15 7/10/14 4:22 PM
If you refresh your web browser (still viewing [BLOG_URL]index.php ) you will see a
Welcome to Recess! message. If you do not see this message, be sure you append
index.php to the URL. The SQLite database file will be created for us at
[BLOG_ROOT]data/sqlite/default.db . Make sure this database file is writeable
by the web server, too. Finally, run these SQL commands in the SQLite database.
These commands create the blog schema and load sample data. If you are
unfamiliar with sqlite on the command line, you can download the Firefox SQLite
manager extension that provides an easy-to-use GUI inteface.
CREATE TABLE posts (
id INTEGER PRIMARY KEY,
title TEXT,
content TEXT,
created INTEGER,
updated INTEGER
NEWMEDIACAMPAIGNS
About Services Work Blog Labs Contact
How to create a blog with the Recess PHP Framework, Part 1 http://www.newmediacampaigns.com/page/how-to-create-a-b...
4 of 15 7/10/14 4:22 PM
Create the blog applicationThe Recess Framework supports multiple modular applications, similar to Django.
We need to create a new application for our blog and tell Recess to use our
application. First, create the necessary directory structure for our blog application:
After we create this directory structure, we need to create the blog application
configuration file at [BLOG_ROOT]apps/blog/BlogApplication.class.php . There are
);
INSERT INTO posts VALUES (1, 'Adventure Book', 'This is the content of
the adventure book', 1254940395, 1254940395);
INSERT INTO posts VALUES (2, 'Romance Book', 'This is the content of the
romance book', 1254940395, 1254940395);
INSERT INTO posts VALUES (3, 'History Book', 'This is the content of the
history book', 1254940395, 1254940395);
[BLOG_ROOT]apps/blog/
[BLOG_ROOT]apps/blog/controllers/
[BLOG_ROOT]apps/blog/models/
[BLOG_ROOT]apps/blog/views/
NEWMEDIACAMPAIGNS
About Services Work Blog Labs Contact
How to create a blog with the Recess PHP Framework, Part 1 http://www.newmediacampaigns.com/page/how-to-create-a-b...
5 of 15 7/10/14 4:22 PM
several important details to note. First, capitalization is important; see how we
capitalized the B and the A in BlogApplication. Next, it is also important that we
append .class.php to the file name. Like Java, this file extension emphasizes the
one class per file convention. Open BlogApplication.class.php in a text editor
and insert the following code:
The specific details in this file are not important for this tutorial. Just know that this
file is the configuration file for our Recess blog application. Finally, we need to tell
Recess to use our blog application. Open [BLOG_ROOT]recess-conf.php in a text
editor. We need to append our blog application to the RecessConf::$applications
array. Locate this array and insert blog.BlogApplication as a new array item. The
final array should look like this:
The initial blog. prefix represents the [BLOG_ROOT]apps/blog/ directory. The
BlogApplication suffix represents the BlogApplication.class.php file we
created earlier.
<?php
Library::import('recess.framework.Application');
class BlogApplication extends Application {
public function __construct() {
$this->name = 'Blog';
$this->viewsDir = $_ENV['dir.apps'] . 'blog/views/';
$this->modelsPrefix = 'blog.models.';
$this->controllersPrefix = 'blog.controllers.';
$this->routingPrefix = 'blog/';
}
}
?>
RecessConf::$applications = array(
'recess.apps.tools.RecessToolsApplication',
'welcome.WelcomeApplication',
'blog.BlogApplication'
);
NEWMEDIACAMPAIGNS
About Services Work Blog Labs Contact
How to create a blog with the Recess PHP Framework, Part 1 http://www.newmediacampaigns.com/page/how-to-create-a-b...
6 of 15 7/10/14 4:22 PM
Create the Post modelThe Post model will interact with the database using Recessʼ ORM (Object
Relational Mapper). Each record in the posts database table will be mapped to a
Post model in our application. First, create [BLOG_ROOT]apps/blog/models
/Post.class.php . Open this file in a text editor and insert the following code:
Recess uses declarative annotations to attach metadata to models, controllers, and
controller methods (similar to PHPDoc annotations, but prefixed with ! instead of @).
In the Post model above, we use a Recess annotation to declare the database
table used by the Post model.
Create the Post controllerThe Post controller receives requests, interacts with the Post model, and returns
a response. First, create [BLOG_ROOT] apps/blog/controllers
/PostsController.class.php . Open this file in a text editor and insert the following
code:
<?php
/**
* !Table posts
*/
class Post extends Model {}
?>
<?php
Library::import('recess.framework.controllers.Controller');
Library::import('blog.models.Post');
/**
* !RespondsWith Layouts
* !Prefix Routes: /, Views: home/
*/
class PostsController extends Controller {
NEWMEDIACAMPAIGNS
About Services Work Blog Labs Contact
How to create a blog with the Recess PHP Framework, Part 1 http://www.newmediacampaigns.com/page/how-to-create-a-b...
7 of 15 7/10/14 4:22 PM
Letʼs walk through the code line by line.
Line 2: We import the Controller super class
Line 3: We import the Post model class
Line 6: A Recess annotation that declares the View used by the
PostsController
Line 7: A Recess annotation that declares the Routing prefix and View prefix
for the PostsController
Line 9: We instantiate the PostsController class
Listing postsLetʼs create a controller action in PostsController and a view template that list all
blog posts:
First we instantiate a new Post object. Next we call $post->all() to retrieve an
array of Post records from the database. We store this array in a public class
variable, $this->posts . All public class variables in the controller are automatically
passed into the view template which we will create next. We also attach a Recess
annotation to the PostsController::listPosts() action. This Recess annotation
declares the routing information for this action: we can access this action with a GET
request to [BLOG_URL]index.php/blog/posts/. We can view the
PostsController::listPosts() controller action in a web browser at
//insert actions here
}
?>
/** !Route GET, posts */
public function listPosts(){
$post = new Post();
$this->posts = $post->all();
}
NEWMEDIACAMPAIGNS
About Services Work Blog Labs Contact
How to create a blog with the Recess PHP Framework, Part 1 http://www.newmediacampaigns.com/page/how-to-create-a-b...
8 of 15 7/10/14 4:22 PM
[BLOG_URL]index.php/blog/posts/ . If you view this action in a web browser, you
will receive an error because we have not yet created our view template. Letʼs do
that now.
View templates reside in the [BLOG_ROOT]apps/blog/views/ directory. Since our
PostsController ʼs Views Prefix is home (declared as a Recess annotation on the
controller), our view templates will be created in [BLOG_ROOT]apps/blog/views
/home/ . Letʼs create the template for the PostsController::listPosts() action.
Create the file [BLOG_ROOT]apps/blog/views/home/listPosts.html.php . Insert the
following HTML markup into this file:
<html>
<body>
<h1>Posts</h1>
<ul>
<?php foreach( $posts as $post ): ?>
<li>
<h2><a href="#"><?php echo $post->title; ?></a></h2>
<p>Posted on <?php echo strftime("%B %e, %Y",
$post->created); ?></p>
<p><?php echo $post->content; ?></p>
</li>
<?php endforeach; ?>
</ul>
</body>
</html>
NEWMEDIACAMPAIGNS
About Services Work Blog Labs Contact
How to create a blog with the Recess PHP Framework, Part 1 http://www.newmediacampaigns.com/page/how-to-create-a-b...
9 of 15 7/10/14 4:22 PM
In your web browser, view [BLOG_URL]index.php/blog/posts/ . You should see a
list of posts. Our view markup loops over the $posts variable (passed from the
controller). Columns from the posts database table are available as object
methods. You may notice that we did not set the href attribute of the post title link
(we will do this next). First, we need to create a new controller method to display a
single blog post. Letʼs do that now.
Showing a single postCreate a new PostsController action called show:
/** !Route GET, posts/$id */
public function show($id){
NEWMEDIACAMPAIGNS
About Services Work Blog Labs Contact
How to create a blog with the Recess PHP Framework, Part 1 http://www.newmediacampaigns.com/page/how-to-create-a-b...
10 of 15 7/10/14 4:22 PM
This method uses a Dynamic Route which passes the $id route parameter into the
PostsController::show() action. If we access [BLOG_URL]index.php/blog
/posts/1 , the PostsController::show() actionʼs first parameter will be equal to 1.
We then set a public class variable — $this->post — equal to the first Post whose
ID is equal to $id . The $this->post variable will be passed into the view
template.
Create the view template file [BLOG_ROOT]apps/blog/views/home/show.html.php
and insert this HTML markup:
Next, we need to edit [BLOG_ROOT]apps/blog/views/home/listPosts.html.php and
$post = new Post($id);
$this->post = $post->find()->first();
}
<html>
<body>
<h1><?php echo $post->title; ?></h1>
<p>Posted on <?php echo strftime("%B %e, %Y", $post->created);
?></p>
<p><?php echo $post->content; ?></p>
</body>
</html>
NEWMEDIACAMPAIGNS
About Services Work Blog Labs Contact
How to create a blog with the Recess PHP Framework, Part 1 http://www.newmediacampaigns.com/page/how-to-create-a-b...
11 of 15 7/10/14 4:22 PM
set the href attribute of each Postʼs title. Update the HTML markup so that it
reads:
We are using the Url helper available in our view template. The first parameter of
the Url::action() method is a Controller::method pair, or the destination of our
hyperlink. The second parameter is the ID of the current Post. The Url::action()
helper method creates a URL to the PostsController::show() controller action
using the current Postʼs ID. View [BLOG_URL]index.php/blog/posts/ in a web
browser. You can now click on a postʼs title to view the postʼs individual page.
SummaryIn this article we learned how to install and configure the Recess Framework, how to
create a modular Recess application, how to create a model that interacts with a
database table, how to create a controller, and how to create a view template. We
now have a basic blog that displays a list of posts with links to each postʼs individual
page. The next article of this series will demonstrate how to create posts, update
posts, and delete posts using the Recess Framework ORM. Stay tuned!
<html>
<body>
<h1>Posts</h1>
<ul>
<?php foreach( $posts as $post ): ?>
<li>
<h2><a href="<?php echo
Url::action('PostsController::show',$post->id); ?>"><?php echo
$post->title; ?></a></h2>
<p>Posted on <?php echo strftime("%B %e, %Y",
$post->created); ?></p>
<p><?php echo $post->content; ?></p>
</li>
<?php endforeach; ?>
</ul>
</body>
</html>
NEWMEDIACAMPAIGNS
About Services Work Blog Labs Contact
How to create a blog with the Recess PHP Framework, Part 1 http://www.newmediacampaigns.com/page/how-to-create-a-b...
12 of 15 7/10/14 4:22 PM
Download the final projectThis file contains the Recess Framework and all controllers, models, and views for
this tutorial.
Download (ZIP, 340 KB)
Related Posts
Community Fosters CreativityJul 2, 2014 | 2 Comments
Why a Mobile Friendly Websiteis Important for Non-ProfitsJun 30, 2014
The Value of a Blog for a LawFirmJun 26, 2014
Comments
's avatar nmc team memberMeHow-to MANUALLY create a blog with the Recess PHP Framework.
10.26.2009
Custom Web Application DevelopmentHi There,I've been playing with the Recess! PHP Framework recently and I like itThanks,
NEWMEDIACAMPAIGNS
About Services Work Blog Labs Contact
How to create a blog with the Recess PHP Framework, Part 1 http://www.newmediacampaigns.com/page/how-to-create-a-b...
13 of 15 7/10/14 4:22 PM
Leave a comment
Mick
01.13.2011
Name *
Email *
Website URL
Is water wet or dry? *
Comment *
SUBMIT COMMENT
CONTACT NMC
118 E. Main St. Suite A
NEWMEDIACAMPAIGNS
About Services Work Blog Labs Contact
How to create a blog with the Recess PHP Framework, Part 1 http://www.newmediacampaigns.com/page/how-to-create-a-b...
14 of 15 7/10/14 4:22 PM
Carrboro, NC 27510(919) [email protected]
EMAIL ADDRESS...
STAY IN TOUCH Sign up to receive our monthly newsletter
About Services Work Blog Labs ContactNEWMEDIACAMPAIGNS
NEWMEDIACAMPAIGNS
About Services Work Blog Labs Contact
How to create a blog with the Recess PHP Framework, Part 1 http://www.newmediacampaigns.com/page/how-to-create-a-b...
15 of 15 7/10/14 4:22 PM