Skip to content

Commit

Permalink
Remove objects from request attributes
Browse files Browse the repository at this point in the history
  • Loading branch information
Herzult committed Jan 7, 2012
1 parent 3da80f2 commit 816823d
Show file tree
Hide file tree
Showing 5 changed files with 93 additions and 25 deletions.
61 changes: 50 additions & 11 deletions Controller/PostController.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,21 +10,30 @@

class PostController extends Controller
{
public function newAction(Topic $topic)
public function newAction($categorySlug, $slug)
{
$form = $this->get('herzult_forum.form.post');

$template = sprintf('%s:new.html.%s', $this->container->getParameter('herzult_forum.templating.location.post'), $this->getRenderer());
return $this->get('templating')->renderResponse($template, array(
'form' => $form->createView(),
'topic' => $topic,
));
$topic = $this->findTopicOr404($categorySlug, $slug);
$form = $this->get('herzult_forum.form.post');
$template = sprintf(
'%s:new.html.%s',
$this->container->getParameter('herzult_forum.templating.location.post'),
$this->getRenderer()
);

return $this->render(
$template,
array(
'form' => $form->createView(),
'topic' => $topic,
)
);
}

public function createAction(Topic $topic)
public function createAction($categorySlug, $slug)
{
$form = $this->get('herzult_forum.form.post');
$post = $this->get('herzult_forum.repository.post')->createNewPost();
$topic = $this->findTopicOr404($categorySlug, $slug);
$form = $this->get('herzult_forum.form.post');
$post = $this->get('herzult_forum.repository.post')->createNewPost();
$post->setTopic($topic);
$form->bindRequest($this->get('request'));

Expand Down Expand Up @@ -65,6 +74,36 @@ public function deleteAction($id)
return new RedirectResponse($this->get('herzult_forum.router.url_generator')->urlForPost($precedentPost));
}

protected function findTopicOr404($categorySlug, $slug)
{
$category = $this
->get('herzult_forum.repository.category')
->findOneBySlug($categorySlug)
;

if (null === $category) {
throw new NotFoundHttpException(sprintf(
'The category with slug "%s" was not found.',
$categorySlug
));
}

$topic = $this
->get('herzult_forum.repository.topic')
->findOneByCategoryAndSlug($category, $slug)
;

if (null === $topic) {
throw new NotFoundHttpException(sprintf(
'The topic with slug "%s" was not found in category with slug "%s".',
$slug,
$categorySlug
));
}

return $topic;
}

protected function getRenderer()
{
return $this->container->getParameter('herzult_forum.templating.engine');
Expand Down
51 changes: 40 additions & 11 deletions Controller/TopicController.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public function newAction(Category $category = null)
$topic->setCategory($category);
}
$form->setData($topic);

$template = sprintf('%s:new.html.%s', $this->container->getParameter('herzult_forum.templating.location.topic'), $this->getRenderer());
return $this->get('templating')->renderResponse($template, array(
'form' => $form->createView(),
Expand Down Expand Up @@ -58,12 +58,14 @@ public function createAction(Category $category = null)
return new RedirectResponse($url);
}

public function listAction(Category $category = null, array $pagerOptions)
public function listAction($categorySlug, array $pagerOptions)
{
if (null !== $category) {
$topics = $this->get('herzult_forum.repository.topic')->findAllByCategory($category, true);
if (null === $categorySlug) {
$category = null;
$topics = $this->get('herzult_forum.repository.topic')->findAll(true);
} else {
$topics = $this->get('herzult_forum.repository.topic')->findAll(true);
$category = $this->findCategoryOr404($categorySlug);
$topics = $this->get('herzult_forum.repository.topic')->findAllByCategory($category, true);
}

$topics->setCurrentPage($pagerOptions['page']);
Expand Down Expand Up @@ -100,16 +102,18 @@ public function showAction($categorySlug, $slug)

public function postNewAction($categorySlug, $slug)
{
$topic = $this->findTopic($categorySlug, $slug);

return $this->forward('HerzultForumBundle:Post:new', array('topic' => $topic));
return $this->forward('HerzultForumBundle:Post:new', array(
'categorySlug' => $categorySlug,
'slug' => $slug
));
}

public function postCreateAction($categorySlug, $slug)
{
$topic = $this->findTopic($categorySlug, $slug);

return $this->forward('HerzultForumBundle:Post:create', array('topic' => $topic));
return $this->forward('HerzultForumBundle:Post:create', array(
'categorySlug' => $categorySlug,
'slug' => $slug
));
}

public function deleteAction($id)
Expand Down Expand Up @@ -148,4 +152,29 @@ public function findTopic($categorySlug, $topicSlug)

return $topic;
}

/**
* Finds the category having the specified slug or throws a 404 exception
*
* @param string $slug
*
* @return Category
* @throws NotFoundHttpException
*/
protected function findCategoryOr404($slug)
{
$category = $this
->get('herzult_forum.repository.category')
->findOneBySlug($slug)
;

if (!$category) {
throw new NotFoundHttpException(sprintf(
'The category with slug "%s" was not found.',
$slug
));
}

return $category;
}
}
2 changes: 1 addition & 1 deletion Resources/views/Category/show.html.twig
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
<div class="main topics">
<h2>{{ category.name }}</h2>
<a href="{{ forum_urlForCategoryAtomFeed(category) }}" title="Atom feed" class="feed atom">Atom feed</a>
{% render 'HerzultForumBundle:Topic:list' with {'category': category, 'pagerOptions': {'page': page, 'routeName': 'herzult_forum_category_show', 'routeParams': {'slug': category.slug}}} %}
{% render 'HerzultForumBundle:Topic:list' with {'categorySlug': category.slug, 'pagerOptions': {'page': page, 'routeName': 'herzult_forum_category_show', 'routeParams': {'slug': category.slug}}} %}
</div>
<div class="side categories">
<h2>Categories</h2>
Expand Down
2 changes: 1 addition & 1 deletion Resources/views/Category/show.xml.twig
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<?xml version="1.0" encoding="UTF-8"?>
<feed xmlns="http://www.w3.org/2005/Atom">
<title>{{ category.name }} Last Topics</title>
{% render 'HerzultForumBundle:Topic:list' with {'category': category} %}
{% render 'HerzultForumBundle:Topic:list' with {'categorySlug': category.slug, 'pagerOptions': {'page': page}} %}
</feed>
2 changes: 1 addition & 1 deletion Resources/views/Forum/index.html.twig
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
</ul>
<div class="main topics">
<h2>Latest topics</h2>
{% render 'HerzultForumBundle:Topic:list' with {'category': null, 'pagerOptions': {'page': page}} %}
{% render 'HerzultForumBundle:Topic:list' with {'categorySlug': null, 'pagerOptions': {'page': page}} %}
</div>
<div class="side categories">
<h2>Categories</h2>
Expand Down

0 comments on commit 816823d

Please sign in to comment.