Creating a Simple Blog Site
Initialize the Data Model
1. To create the database we are using YAML syntax.
2. First need to create schema.yml file in sf_sandbox/config/ with the following contents:
propel:
blog_post:
id: ~
title: varchar(255)
excerpt: longvarchar
body: longvarchar
created_at: ~
blog_comment:
id: ~
blog_post_id: ~
author: varchar(255)
email: varchar(255)
body: longvarchar
created_at: ~
3. Save the file, open a command line, browse to the sf_sandbox/ directory and type:
php symfony propel:build-model
Make sure your command line folder is set to the root of your project (eg. sf_sandbox) when you call symfony command.
4. When you invoke that command, symfony will generate the classes under the sf_sandbox/lib/model/ directory.
5. Now, we need to convert the schema to SQL statements to initialize the database tables. By default, the symfony sandbox is configured to work out of the box with a simple SQLite file, so no database initialization is required. You still need to check that the SQLite extension is installed and enabled correctly (you can check this in php.ini).
6. By default, the sf_sandbox project will use a database file called sandbox.db located in sf_sandbox/data/.
7. Change the attribute of sf_sandbox/data/sandbox.db to writable.
8. Build the tables by using the command propel:build-sql as follows:
php symfony propel:build-sql
9. A lib.model.schema.sql file is created in sf_sandbox/data/sql/.
10. To build the table structure based on the the SQL file, type:
php symfony propel:insert-sql
11. As we want to be able to create and edit the blog posts and comments, we also need to generate some forms based on our model schema, type the following:
php symfony propel:build-forms
This will generate classes in the sf_sandbox/lib/form/ directory. These classes are used to manage our model objects as forms.
Create the Application
1. The basic features of a blog are to be able to Create, Retrieve, Update and Delete (CRUD) posts and comments. As you are new to symfony, you will not create symfony code from scratch, but rather let it generate the code that you may use and modify as needed. Symfony can interpret the data model to generate the CRUD interface automatically:
php symfony propel:generate-crud --non-verbose-templates --non-atomic-actions --with-show frontend post BlogPost
php symfony propel:generate-crud --non-verbose-templates --non-atomic-actions frontend comment BlogComment
php symfony cache:clear
2. You now have two modules (post and comment) that will let you manipulate objects of the BlogPost and BlogComment classes. A module usually represents a page or a group of pages with a similar purpose. Your new modules are located in the sf_sandbox/apps/frontend/modules/ directory, and they are accessible by the URLs:
http://localhost/sf_sandbox/web/frontend_dev.php/post
http://localhost/sf_sandbox/web/frontend_dev.php/comment
3. Edit the BlogPost class (lib/model/BlogPost.php) and add the __toString() method:
class BlogPost extends BaseBlogPost
{
public function __toString()
{
return $this->getTitle();
}
}
4. Lastly, add the following CSS to sf_sandbox\web\css\main.css:
body, td
{
font-family: Arial, Verdana, sans-serif;
font-size: 12px;
}
td { margin: 4px; padding: 4px; }
5. Now, feel free to create some new posts to make the blog look less empty.
Modify the Layout
In order to navigate between the two new modules, the blog needs some global navigation.
1. Edit the global template sf_sandbox/apps/frontend/templates/layout.php and change the content of the <body> tag to:
<div id="container" style="width:700px;margin:0 auto;border:1px solid grey;padding:10px">
<div id="navigation" style="display:inline;float:right">
<ul>
<li><?php echo link_to('List of posts', 'post/index') ?></li>
<li><?php echo link_to('List of comments', 'comment/index') ?></li>
</ul>
</div>
<div id="title">
<h1><?php echo link_to('My first symfony project', '@homepage') ?></h1>
</div>
<div id="content" style="clear:right">
<?php echo $sf_data->getRaw('sf_content') ?>
</div>
</div>
2. To change the name of your application, edit the view configuration file of the application at sf_sandbox/apps/frontend/config/view.yml then locate the line showing the title key and change it to the title that you want.
3. The home page itself needs to be changed. It uses the default template of the default module, which is kept in the framework but not in your application directory. To override it, you can create a custom main module:
php symfony generate:module frontend main
4. By default, the index action shows a default congratulations screen. To remove it, edit the file at sf_sandbox/apps/frontend/modules/main/actions/actions.class.php and remove the content of the executeIndex() method as follows:
/**
* Executes index action
*
* @param sfRequest $request A request object
*/
public function executeIndex($request)
{
}
5. Edit the sf_sandbox/apps/frontend/modules/main/templates/indexSuccess.php file to show a nice welcome message as follows:
<h1>Welcome to my new blog</h1>
<p>You are the <?php echo rand(1000,5000) ?>th visitor today.</p>
6. To execute your home page edit the sf_sandbox/apps/frontend/config/routing.yml and change the homepage rule as follows:
homepage:
url: /
param: { module: main, action: index }
Enjoy your first application with symfony as:
http://localhost/sf_sandbox/web/frontend_dev.php/
Good Job!!!!