Symfony Apps Part II
Yesterday we have try to build a simple application based on symfony, now we’ll try to make those apps more usable what we have left now is user management
the sequence if users want to make a movie reviews is
- user login to the apps
- user pick which movie he / she want to review
- he / she put some review on it
- he can see the reviews.
in the previous sample, we’ve only build the CRUD application, which base of almost every application. To make it simple, we will use a plugin for symfony called sfGuardPlugin. It’s a plugin to ease the pain of user management and save a lot of our time.
first of all you’ve to download the plugin sfGuardPlugin
and follow the instruction found in http://trac.symfony-project.com/attachment/wiki/sfGuardPlugin/
if you succeeded installed the plugin, you should see the login screen when you trying to access your module.
but the page is a plain one, let’s try to change it a little bit first of all you have to create a folder called sfGuardAuth in your module folder, the module folder it self is on the apps/samples
inside of the sfGuardAuth, create a new folder named templates. Now fire up your favorite editor and create a new PHP file, we have to name it signinSuccess.php and secureSuccess.php
let’s create it like this
<?php use_helper('Validation', 'I18N') ?>
<?php use_helper('Javascript') ?>
<div id="login" style='position: relative;margin: 5em auto 1em;padding: 20px 0 0;width: 425px;'>
<?php echo form_tag('@sf_guard_signin') ?>
<div class="form-row" id="sf_guard_auth_username">
<?php
echo form_error('username');
echo "<p>";
echo label_for('username', __('username:'));
echo "<br/>";
echo input_tag('username', $sf_data->get('sf_params')->get('username'));
echo "</p>";
?>
</div>
<div class="form-row" id="sf_guard_auth_password">
<?php
echo form_error('password');
echo "<p>";
echo label_for('password', __('password:'));
echo "<br/>";
echo input_password_tag('password');
echo "</p>";
?>
</div>
<div class="form-row" id="sf_guard_auth_remember">
<?php
echo label_for('remember', __('Remember me?')),
checkbox_tag('remember');
?>
</div>
<?php
echo submit_tag(__('Sign in'));
echo "<br/>";
echo link_to(__('Forgot your password?'), '@sf_guard_password', array('id' => 'sf_guard_auth_forgot_password'))
?>
</form>
</div>
that’s for the login page, now what we want is user pick the movie and then give their review of the movie.
first of all let’s change the listSuccess.php inside in apps/samples/modules/movie/template, the default identifier from the CRUD generator is ID. So if you click on id, it will go to show page, so we’ll gonna change the listSuccess.php so instead of ID user click on the Movie name.
<?php
// auto-generated by sfPropelCrud
// date: 2007/11/02 14:03:39
?>
<h1>movie</h1>
<table>
<thead>
<tr>
<th>Movie name</th>
<th>Movie synopsis</th>
<th>Movie released</th>
<th>Movie produsen</th>
<th>Movie director</th>
</tr>
</thead>
<tbody>
<?php foreach ($movies as $movie): ?>
<tr>
<td><?php echo link_to($movie->getMovieName(), 'movie/show?id='.$movie->getId()) ?></td>
<td><?php echo $movie->getMovieSynopsis() ?></td>
<td><?php echo $movie->getMovieReleased() ?></td>
<td><?php echo $movie->getMovieProdusen() ?></td>
<td><?php echo $movie->getMovieDirector() ?></td>
</tr>
<?php endforeach; ?>
</tbody>
</table>
<?php echo link_to ('create', 'movie/create') ?>
those are the new listSuccess.php, and we should change a little bit file editSuccess.php,we need to create an hidden field contained user_id add this line below
next is to change the showSuccess.php and this is a new one code snippet.
<?php
....
<hr />
<?php echo link_to('edit', 'movie/edit?id='.$movie->getId()) ?>
<?php echo link_to('list', 'movie/list') ?>
<?php use_helper('Text','Date') ?>
<hr/>
<?php if($reviews) : ?>
<p><?php echo count($reviews) ?> Critic <?php if(count($reviews)>1): ?>s<?php endif; ?>
<?php foreach ($reviews as $review) : ?>
<p><em> Posted by <?php echo $review->getsfGuardUser()->getUserName() ?> On <?php echo format_date($critic->getCreatedAt()) ?> </em></p>
<div class="comment" style="margin-bottom:10px;">
<?php echo simple_format_text($critic->getReviews()) ?>
</div>
<?php endforeach; ?>
<?php endif ?>
<?php echo link_to('Add a review', 'review/create?movie_id='.$movie->getId())
?>
OK we just need to do a little more work in code. let’s edit the action.class.php, this file is inside of apps/samples/movie/action. Edit the function executeShow() to the following
public function executeShow()
{
$this->movie = MoviePeer::retrieveByPk($this->getRequestParameter('id'));
$this->forward404Unless($this->movie);
$c = new Criteria();
$c->add(ReviewsPeer::MOVIE_ID,$this->getRequestParameter('id'));
$c->addAscendingOrderByColumn(ReviewsPeer::CREATED_AT);
$this->reviews= ReviewsPeer::doSelect($c);
}
okay, that would be the last file we need to edit in movie directory. what we need to edit is another action.class.php located in app/samples/module/review and make a little bit change in function executeUpdate() so the function will look like
public function executeUpdate()
{
if (!$this->getRequestParameter('id'))
{
$reviews= new Reviews();
}
else
{
$reviews= ReviewsPeer::retrieveByPk($this->getRequestParameter('id'));
$this->forward404Unless($reviews);
}
$reviews->setId($this->getRequestParameter('id'));
$reviews->setMovieId($this->getRequestParameter('movie_id') ? $this->getRequestParameter('movie_id') : null);
$reviews->setReviews($this->getRequestParameter('reviews'));
$reviews->setRating($this->getRequestParameter('rating'));
$reviews->setUserId($this->getUser()->getGuardUser()->getId());
$reviews->save();
return $this->redirect('movie/show?id='.$reviews->getMovieId());
}
now we’ve done, but the application still need a lot of improvement but i do hope that will give you a hint of how easy is to customize symfony
Permalink Comments off


