2
votes

I want to add Notifications system in my symfony (2.8) project, i thought that Sonata Notification Bundle could help, but turns out that i do not know how to use it, i install it very well, but i do not know how to use it in my project. i need some help about this bundle, some tutorial or so. or is there another way to use notification system, please tell me, thank you in advance That the controller that i want to use notification bundle

namespace LocationBundle\Controller;

use Symfony\Component\HttpFoundation\Request; use Symfony\Bundle\FrameworkBundle\Controller\Controller;

use LocationBundle\Entity\Agence; use Symfony\Component\HttpFoundation\JsonResponse;

/** * Agence controller. * */ class AgenceController extends Controller { /** * Lists all Agence entities. * */ public function indexAction() {
$em = $this->getDoctrine()->getManager();

    $agences = $em->getRepository('LocationBundle:Agence')->findAll();

    return $this->render('agence/index.html.twig', array(
        'agences' => $agences,            
    ));
}

/**
 * Creates a new Agence entity.
 *
 */
public function newAction(Request $request)
{
    $agence = new Agence();
    $form = $this->createForm('LocationBundle\Form\AgenceType', $agence);
    $form->handleRequest($request);

    if ($form->isSubmitted() && $form->isValid()) {
        $em = $this->getDoctrine()->getManager();
        $em->persist($agence);
        $em->flush();

        return $this->redirectToRoute('agence_show', array('id' => $agence->getId()));
    }

    return $this->render('agence/new.html.twig', array(
        'agence' => $agence,
        'form' => $form->createView(),
    ));
}

/**
 * Finds and displays a Agence entity.
 *
 */
public function showAction(Agence $agence)
{
    $deleteForm = $this->createDeleteForm($agence);

    return $this->render('agence/show.html.twig', array(
        'agence' => $agence,
        'delete_form' => $deleteForm->createView(),
    ));
}

/**
 * Displays a form to edit an existing Agence entity.
 *
 */
public function editAction(Request $request, Agence $agence)
{
    $deleteForm = $this->createDeleteForm($agence);
    $editForm = $this->createForm('LocationBundle\Form\AgenceType', $agence);
    $editForm->handleRequest($request);

    if ($editForm->isSubmitted() && $editForm->isValid()) {
        $em = $this->getDoctrine()->getManager();
        $em->persist($agence);
        $em->flush();

        return $this->redirectToRoute('agence_edit', array('id' => $agence->getId()));
    }

    return $this->render('agence/edit.html.twig', array(
        'agence' => $agence,
        'edit_form' => $editForm->createView(),
        'delete_form' => $deleteForm->createView(),
    ));
}

/**
 * Deletes a Agence entity.
 *
 */
public function deleteAction(Request $request, Agence $agence)
{
    $form = $this->createDeleteForm($agence);
    $form->handleRequest($request);

    if ($form->isSubmitted() && $form->isValid()) {
        $em = $this->getDoctrine()->getManager();
        $em->remove($agence);
        $em->flush();
    }

    return $this->redirectToRoute('agence_index');
}

/**
 * Creates a form to delete a Agence entity.
 *
 * @param Agence $agence The Agence entity
 *
 * @return \Symfony\Component\Form\Form The form
 */
private function createDeleteForm(Agence $agence)
{
    return $this->createFormBuilder()
        ->setAction($this->generateUrl('agence_delete', array('id' => $agence->getId())))
        ->setMethod('DELETE')
        ->getForm()
    ;
}
1

1 Answers

2
votes

I am pretty sure the Sonata Notification Bundle is not what you are searching. The Word "Notification" in the title is in your case a bit misleading. The Bundle is used to postpone actions/events using a queue system like RabbitMQ.

For what you are searching: Take a look at the Symfony's own "Flash Messages": http://symfony.com/doc/current/book/controller.html#flash-messages

It's very easy to implement and you don't need an additional bundle.