To build this plugin I’m going to start with a new project, entirely from scratch. This means that I will be initially developing a normal application. Once we’re nearing the end, we’ll package the project up into a plugin. This post will cover the development of basic CRUD functionality so we can actually do something if we want.
To start off, we’ll need our database and web server set up and ready to go. I won’t go into that now and will just assume that you have config/databases.yml and config/propel.ini both set up and ready to go. I also enjoy setting a VirtualHost in apache to handle the application, but that sort of choice is up to you. It won’t matter to the packaged plugin in the end what you name the development database or how to configure your server.
We’ll need to have sfGuardUserPlugin installed because there are a few references to it in the schema of the previous post. Let’s install it now before we set up the database tables.
symfony plugin:install sfGuardPlugin;Now that we have sfGuard installed, we should be ready to build the application.
symfony propel:build-all --no-confirmation;The schema is established in the database and we’re ready to go.
Let’s establish a few generators to take care of data entry. For now, let’s establish one for managing posts and managing comments.
symfony propel:generate-admin --module="post" frontend sfFauxWPPost;
symfony propel:generate-admin --module="comment" frontend sfFauxWPComment;Next let’s set up the post module as the default module (for now) for the frontend, and set up links to the two modules in the template.
...
# default rules
homepage:
url: /
param: { module: post, action: index }
......
<body>
<ul>
<li><?php echo link_to('Posts', '@sf_faux_wp_post') ?></li>
<li><?php echo link_to('Comments', '@sf_faux_wp_comment')?></li>
</ul>
<?php echo $sf_content ?>
</body>
...Now, after cleaing the cache (symfony cc;) we should be able to load the application and see the Post module. It’s rather messy, however, since there is a large fitler box on the side and no default data to look at. Also, if we try to click on the “Comments” link we’ll get a failure because there is no “__toString” function for Posts. Let’s fix this first of all since we’ll be needing to echo objects a lot in this project.
The following are the objects that we’d expect to produce a string when echoed. As for the others, feel free to add a __toString if you want. We’ll probably address these objects later. I am including phpDoc documentation in the functions because I want to have a correctly defined tooltip in NetBeans whne I reference this function.
<?php
class sfFauxWPCategory extends BasesfFauxWPCategory
{
/**
* Echos the name of the category.
*
* @return string
*/
public function __toString()
{
$this->getName();
}
}
class sfFauxWPComment extends BasesfFauxWPComment
{
/**
* Echos the content of the comment.
*
* @return string
*/
public function __toString()
{
$this->getContent();
}
}
<?php
class sfFauxWPPost extends BasesfFauxWPPost
{
/**
* Echos the title of the post.
*
* @return string
*/
public function __toString()
{
$this->getTitle();
}
}
<?php
class sfFauxWPTag extends BasesfFauxWPTag
{
/**
* Echos the name of the tag.
*
* @return string
*/
public function __toString()
{
return $this->getName();
}
}
Now the basic CRUD functions for both Posts and Comments should be working. Finally, before we end for now, let’s establish a “show” view for posts.
<?php
/**
* post actions.
*
* @package PhpProject1
* @subpackage post
* @author Your name here
* @version SVN: $Id: actions.class.php 12474 2008-10-31 10:41:27Z fabien $
*/
class postActions extends autoPostActions
{
public function executeShow(sfWebRequest $request)
{
$this->sf_faux_wp_post = $this->getRoute()->getObject();
}
}
<?php use_helper('I18N', 'Date') ?>
<?php include_partial('post/assets') ?>
<div id="sf_admin_container">
<h1><?php echo __($sf_faux_wp_post->getTitle(), array(), 'messages') ?></h1>
<div id="sf_admin_content">
<div id="sf_faux_wp_published_at">
<?php echo $sf_faux_wp_post->getPublishedAt(); ?>
</div>
<div id="sf_faux_wp_content">
<?php echo __(nl2br($sf_faux_wp_post->getContent())); ?>
</div>
<hr/>
<div id="comments">
<div id="comment_count">
<?php echo ($sf_faux_wp_post->countsfFauxWPComments() ? $sf_faux_wp_post->countsfFauxWPComments() : "No") ?> Comment<?php echo ($sf_faux_wp_post->countsfFauxWPComments() != 1 ? 's' : '') ?>
</div>
<?php foreach($sf_faux_wp_post->getsfFauxWPComments() as $id => $comment):?>
<div id="comment_<?php echo $id ?>">
<div id="comment_name_<?php echo $id ?>">
<?php echo __($comment->getWebsite() ? link_to($comment->getAuthor(), $comment->getWebsite()) : $comment->getAuthor()) ?>
</div>
<div id="comment_content_<?php echo $id ?>">
<?php echo __($comment->getContent()) ?>
</div>
</div>
<?php endforeach; ?>
</div>
</div>
</div>
That’s enough for today. Dang, I never expected that writing these posts would slow down the development so much.
This entry was posted on Saturday, February 6th, 2010 at 2:30 pm by NoCoolName_Tom and is filed under Technology, symfony Framework. You can follow any responses to this entry through the RSS 2.0 feed. You can leave a response, or trackback from your own site.