Thursday 21 August 2008

Symfony sandbox project

I recently heard of the Symfony project. It's an appealing free open-source PHP5 development framework licensed under the MIT license and I decided to give it a go.

For starters I downloaded the sandbox version from:
http://www.symfony-project.org/installation/1_1

I had an easy-PHP installation already up and running on my Windows 2000 system. So simply dropped the sandbox project under my www folder and followed the instructions on the Symfony website using mySQL as database manager:
http://www.symfony-project.org/tutorial/1_1/my-first-project

It was all pretty straight-forward except for a few "catches" detailed below.

#1 - the project uses php.exe on the command line to generate configuration files, PHP classes, etc. You really need to make sure to copy the php.ini file to the folder where the php.exe is located in order for the configuration to be taken into account. You can check which modules are available (and in effect, whether your latest php.ini is getting loaded properly) by typing
php -m
on the command line.

#2 - I also activated the xsl extension. This gets rid of some error messages that occur during database configuration and creation. I am not sure whether this makes an actual difference on the end result but was trying to clear any errors as I ran into them.

#3 - When I tried to access http://localhost/sf_sandbox/web/frontend_dev.php/comment or http://localhost/sf_sandbox/web/frontend_dev.php/post as specified in the tutorial, the pages wouldn't launch properly, I was getting database error messages.
To avoid this, I had to manually configure the www\sf_sandbox\config\database.yml configuration file:

all:
propel:
class: sfPropelDatabase
param:
phptype: mysql
database: symfony_project
dsn: 'mysql://root@localhost/symfony_project'

I changed all three parameters: phptype, database (use the database name you chose), and dsn (note that root is using an empty password to connect here).
For changes to take effect, make sure to clear the cache using:
php symfony cache:clear

#4 - In the middle of the tutorial, it tells you to start using your new website.
However, when you try to add a comment you get the following error:
Class "Post" must implement a "__toString" method to be rendered in a "sfWidgetFormPropelSelect" widget
To fix this, open the
sf_sandbox\lib\model\Post.php
and add a __toString method as follows:


class Post extends BasePost
{
function __toString()
{
return $this->getTitle();
}
}


To be 100% accurate, also change the sf_sandbox\apps\frontend\modules\comment\templates\editSuccess.php file so that the Post id label becomes Post title or simply Post since the post title is what we're actually seeing now we have added the __toString method in the Post class.

#5 - I couldn't get the following to work:

Next, open the modules/comment/templates/editSuccess.php and replace the following lines:

<tr>
<th>Post:
<td><?php echo object_select_tag($comment, 'getPostId', array (
'related_class' => 'Post',
)) ?></td>
</tr>


The original code is slightly different in the sandbox version and the replacement code wouldn't work. One way to avoid this issue is to set the default post id in your comment actions class.
Simply open your sf_sandbox\apps\frontend\modules\comment\actions\actions.class.php and add the following in the "executeCreate()" method:

$post=PostPeer::retrieveByPk(
$this->getRequestParameter('post_id'));
if ($post)
{
$this->form->setDefault('post_id',
$this->getRequestParameter('post_id'));
}

This simply pre-selects the post title in the form when you access the 'new comment' page from a given post's page by setting the default value of the drop-down choice list.

#6 - in the same file (sf_sandbox\apps\frontend\modules\comment\actions\actions.class.php). Instead of using the code from the tutorial, simply replace the redirect line with:
$this->redirect('post/show?id='.
$request->getParameter('comment[post_id]'));

This will redirect to the correct post rather than to the comment page.

Well, that's it for now. I haven't finished the tutorial yet. I have only made it to just before the Form Validation chapter. So still lots of fun to be had - stay tuned!

Thoughts in the meantime?

[EDIT: it looks like the Symfony Sandbox Project tutorial has been updated so some - if not all - of the above information may no longer be applicable.]

No comments:

Online Marketing
Add blog to our blog directory blog search directory Blog Directory Blogarama - The Blog Directory