Skip to content

Commit

Permalink
Add flash messages
Browse files Browse the repository at this point in the history
  • Loading branch information
Barvoj committed Oct 29, 2016
1 parent 1857da0 commit 526fc71
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 3 deletions.
9 changes: 9 additions & 0 deletions app/Resources/views/base.html.twig
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,15 @@
</div>

<div class="col-md-9">

{% for flash_message in app.session.flashBag.get('notice') %}


<div class="alert alert-info">
{{ flash_message }}
</div>
{% endfor %}

{% block body %}
{% endblock %}
</div>
Expand Down
10 changes: 9 additions & 1 deletion src/AppBundle/Controller/ContactController.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
use Symfony\Component\Form\FormFactory;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Session\Flash\FlashBag;
use Symfony\Component\Routing\RouterInterface;

/**
Expand All @@ -26,16 +27,21 @@ class ContactController
/** @var RouterInterface */
private $router;

/** @var FlashBag */
private $flashBag;

/**
* @param FormFactory $formFactory
* @param MessageFacade $messageFacade
* @param RouterInterface $router
* @param FlashBag $flashBag
*/
public function __construct(FormFactory $formFactory, MessageFacade $messageFacade, RouterInterface $router)
public function __construct(FormFactory $formFactory, MessageFacade $messageFacade, RouterInterface $router, FlashBag $flashBag)
{
$this->formFactory = $formFactory;
$this->messageFacade = $messageFacade;
$this->router = $router;
$this->flashBag = $flashBag;
}

/**
Expand All @@ -51,6 +57,8 @@ public function contactAction(Request $request)
if ($form->isSubmitted() && $form->isValid()) {
$this->messageFacade->save($messageVO);

$this->flashBag->add('notice', 'Message send.');

return RedirectResponse::create($this->router->generate("contact"));
}

Expand Down
14 changes: 12 additions & 2 deletions src/AppBundle/Controller/FaqController.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
use Symfony\Component\Form\FormFactory;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Session\Flash\FlashBag;
use Symfony\Component\Routing\RouterInterface;

/**
Expand All @@ -34,20 +35,25 @@ class FaqController
/** @var RouterInterface */
private $router;

/** @var FlashBag */
private $flashBag;

/**
* @param FormFactory $formFactory
* @param QuestionFacade $questionFacade
* @param AnswerFacade $answerFacade
* @param FaqFacade $faqFacade
* @param RouterInterface $router
* @param FlashBag $flashBag
*/
public function __construct(FormFactory $formFactory, QuestionFacade $questionFacade, AnswerFacade $answerFacade, FaqFacade $faqFacade, RouterInterface $router)
public function __construct(FormFactory $formFactory, QuestionFacade $questionFacade, AnswerFacade $answerFacade, FaqFacade $faqFacade, RouterInterface $router, FlashBag $flashBag)
{
$this->formFactory = $formFactory;
$this->questionFacade = $questionFacade;
$this->answerFacade = $answerFacade;
$this->faqFacade = $faqFacade;
$this->router = $router;
$this->flashBag = $flashBag;
}

/**
Expand Down Expand Up @@ -95,6 +101,8 @@ public function addAction(Request $request)
if ($form->isSubmitted() && $form->isValid()) {
$this->faqFacade->insert($faqVO);

$this->flashBag->add('notice', 'Question added.');

return RedirectResponse::create($this->router->generate("faq_list"));
}

Expand Down Expand Up @@ -122,11 +130,13 @@ public function editAction($id, Request $request)
if ($form->isSubmitted() && $form->isValid()) {
$this->faqFacade->update($question, $answer, $faqVO);

$this->flashBag->add('notice', 'Question uppdated.');

return RedirectResponse::create($this->router->generate("faq_list"));
}

return [
"form" => $form->createView()
];
}
}
}

0 comments on commit 526fc71

Please sign in to comment.