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 Episode

Get the code for this episode.

Share This Episode

If you have found this video helpful, please consider sharing. I really appreciate it.

Episodes in this series

# Title Duration
1 Intro with Twig CRUD - List 06:03
2 Twig CRUD - Create 03:25
3 Twig CRUD - Update (Edit) 02:43
4 Twig CRUD - Delete 02:36
5 Simple Symfony 3 RESTful API Setup 05:32
6 API - GET a Single BlogPost 04:51
7 API - GET a Collection of BlogPosts 02:20
8 API - POST to Create New Data 06:30
9 API - PUT and PATCH to Update 04:11
10 API - DELETE 03:34
11 Angular - Setup, Styling, and GET All 09:19
12 Angular - Refactoring 10:23
13 Angular - Create (POST) 06:54
14 Angular - Update (PUT) 08:41
15 Angular - Remove (DELETE) 06:44
16 React - Intro, Setup, and GET all 06:54
17 React - Refactoring 09:14
18 React Router 04:41
19 React - Create (POST) 13:13
20 React - Update (PUT / PATCH) 12:40
21 React - Delete 11:00
22 React - Tidy Up, and Finish 04:14