AppBundle\Controller\Admin\BlogController::newAction PHP Method

newAction() public method

Creates a new Post entity.
public newAction ( Request $request )
$request Symfony\Component\HttpFoundation\Request
    public function newAction(Request $request)
    {
        $post = new Post();
        $post->setAuthorEmail($this->getUser()->getEmail());
        // See http://symfony.com/doc/current/book/forms.html#submitting-forms-with-multiple-buttons
        $form = $this->createForm('AppBundle\\Form\\PostType', $post)->add('saveAndCreateNew', 'Symfony\\Component\\Form\\Extension\\Core\\Type\\SubmitType');
        $form->handleRequest($request);
        // the isSubmitted() method is completely optional because the other
        // isValid() method already checks whether the form is submitted.
        // However, we explicitly add it to improve code readability.
        // See http://symfony.com/doc/current/best_practices/forms.html#handling-form-submits
        if ($form->isSubmitted() && $form->isValid()) {
            $post->setSlug($this->get('slugger')->slugify($post->getTitle()));
            $entityManager = $this->getDoctrine()->getManager();
            $entityManager->persist($post);
            $entityManager->flush();
            // Flash messages are used to notify the user about the result of the
            // actions. They are deleted automatically from the session as soon
            // as they are accessed.
            // See http://symfony.com/doc/current/book/controller.html#flash-messages
            $this->addFlash('success', 'post.created_successfully');
            if ($form->get('saveAndCreateNew')->isClicked()) {
                return $this->redirectToRoute('admin_post_new');
            }
            return $this->redirectToRoute('admin_post_index');
        }
        return $this->render('admin/blog/new.html.twig', array('post' => $post, 'form' => $form->createView()));
    }