Twig CRUD - Create

In this video we will implement the createAction, which will be almost identical to how we implement the POST verb in our forthcoming RESTful API implementation.

If any of this is new to you, or there are perhaps parts you are unsure of, be sure to watch this series on The Beginners Guide To Symfony 3 Forms.

The overview of this action, should the name not give the game away, is to allow the creation of new BlogPost entities. This will be achieved by presenting the user with a Symfony form, rendered as HTML via Twig, which the user can fill in and submit back.

As we aren't trying to be fancy here, the rendered form will simply submit (via POST) the form contents back to the createAction, where if successful, the user will be redirected to the listAction / list view.

This means there will be a 'clunky' page refresh once a user submits their data, whether this will be to show some form errors, or successfully redirect to the list view. As we aren't using any entity validation at this stage, the HTML5 form validation should be sufficient to stop the user from submitting an empty form.

     * @param Request $request
     * @Route("/create", name="create")
    public function createAction(Request $request)
        $form = $this->createForm(BlogPostType::class);


        if ($form->isSubmitted() && $form->isValid()) {

             * @var $blogPost BlogPost
            $blogPost = $form->getData();

            $em = $this->getDoctrine()->getManager();

            // for now
            return $this->redirectToRoute('edit', [
                'blogPost' => $blogPost->getId(),


        return $this->render('BlogPosts/create.html.twig', [
            'form' => $form->createView()

In my opinion, there is a perception held by some developers that PHP cannot do 'modern' web applications. I can understand this point, from a certain perspective - given that the default implementation of a site using Symfony with Twig does lend towards this old-school feel - as in click > refresh page > click.

However, this really is just one implementation.

PHP is more than capable of being the back-end to a modern front-end framework.

Twig is great for quickly building out CRUD-like applications that work. They may not have all the bells and whistles of slick and modern frameworks, but as you can see from this, the previous, and the next two videos, Twig really does a great job of making these steps easy for you.

But those investigating whether to use Symfony may only see the Twig approach, and then dismiss Symfony, or worse, PHP in general by association.

What I am aiming to show you in this series is that - given a different implementation - all the existing knowledge and skills you have PHP can be used to build the business end of your application. Then you can make use of React, Ember, Angular, Ionic, Electron, or the 16 other new JavaScript frameworks that have been announced since I started writing this post ;)

Anyway, I am not trying to convince those whose minds are already made up. Instead, I am hoping that if you are open to the idea that PHP has the potential to be your back-end, then Symfony is as good a platform as any to build on.

Thanks for watching / reading, and I will get off my soapbox now.

Code For This Video

Get the code for this video.