public function indexAction() { return $this->redirect($this->generateUrl('homepage'), 301); }
use Symfony\Component\HttpFoundation\RedirectResponse; return new RedirectResponse($this->generateUrl('homepage'));
404error page
public function indexAction() { // retrieve the object from database $product = ...; if (!$product) { throw $this->createNotFoundException('The product does not exist'); } return $this->render(...); }
Session.
use Symfony\Component\HttpFoundation\Request; public function indexAction(Request $request) { $session = $request->getSession(); // store an attribute for reuse during a later user request $session->set('foo', 'bar'); // get the attribute set by another controller in another request $foobar = $session->get('foobar'); // use a default value if the attribute doesn't exist $filters = $session->get('filters', array()); }
Flash message
use Symfony\Component\HttpFoundation\Request; public function updateAction(Request $request) { $form = $this->createForm(...); $form->handleRequest($request); if ($form->isValid()) { // do some sort of processing $request->getSession()->getFlashBag()->add( 'notice', 'Your changes were saved!' ); return $this->redirect($this->generateUrl(...)); } return $this->render(...); }
Flash message in layout
{% for flashMessage in app.session.flashbag.get('notice') %}
class="flash-notice">
{{ flashMessage }}
{% endfor %}
No comments:
Post a Comment