21.10.2015 Views

1-33

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Symfony2 – Franz Jordán 2011<br />

<br />

<br />

<br />

<br />

<br />

<br />

<br />

You've now introduced a methodology that allows for the reuse of the layout. Unfortunately, to<br />

accomplish this, you're forced to use a few ugly PHP functions<br />

(ob_start(), ob_get_clean()) in the template. Symfony2 uses a Templating component<br />

that allows this to be accomplished cleanly and easily. You'll see it in action shortly.<br />

Adding a Blog "show" Page<br />

The blog "list" page has now been refactored so that the code is better-organized and reusable.<br />

To prove it, add a blog "show" page, which displays an individual blog post identified by<br />

an id query parameter.<br />

To begin, create a new function in the model.php file that retrieves an individual blog result<br />

based on a given id:<br />

// model.php<br />

function get_post_by_id($id)<br />

{<br />

$link = open_database_connection();<br />

$id = mysql_real_escape_string($id);<br />

$query = 'SELECT date, title, body FROM post WHERE id = '.$id;<br />

$result = mysql_query($query);<br />

$row = mysql_fetch_assoc($result);<br />

17

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!