Codementor Events

Symfony 3: Remixing the Blog Demo (Part 1)

Published Dec 14, 2016Last updated Jan 18, 2017
Symfony 3: Remixing the Blog Demo (Part 1)

Background

Recently, I’ve been playing around with the newest version of the Symfony framework (3.2). With the release of version 3 also came a sample application that basically shows off the power and sophistication of the framework by demonstrating the suggested conventions that come with it. This was a great addition to the documentation because it acts as a living, breathing example of the best practices mentioned in the Symfony Best Practices Handbook.

In the blog app demo, there are two different bundles containing a variety of components that together form some larger piece of all the application’s features: AppBundle & CodeExplorerBundle. Here is a basic run-down of the components and their related concepts promoted in the Symfony3 Blog demo app:

  • Various command utilities to be executed at the console (via php bin/console app:{command name} [options])
  • An advanced controller setup to handle the backend and frontend side of the application utilizing Doctrine-style annotations to configure things like routes, cache, security as well as define allowed methods and required parameters needed from the request.
  • A standard set of entities using Doctrine ORM to configure specific fields, datatypes, and other database schema settings. They are also defined using annotations (which is much better than separate YML files to define each entity — it makes more sense to keep the entities that are stored in the database themselves together with the code that uses them (annotations). In this way, we eliminate an unnecessary layer to the application that would otherwise require additional files and additional steps (units of work) in the doctrine’s core engine (via commands). Entities include:
    • Post
    • User
    • Comment
  • A (rather limited) demonstration of Symfony’s Event system in the form of an EventListener (specifically, the RedirectToPreferredLocalListener).
  • Basic CRUD operations on the entities via a variety of Symfony Form and FormType implementations that create & manipulate the entities in the database (although not directly but by delegating these changes to repositories).
  • Entity repositories that are configured into doctrine and made accessible to other components through Doctrine’s EntityManager.
  • A twig extension that adds a new filter md2html accessible inside twig templates that transform markdown contents into HTML easily.
  • A few additional utility classes that handle a few specific tasks for the application (Markdown, MomentFormatConverter, and Slugger).
  • A solid services definition file (sevices.yml) that defines the application’s core architecture as well as its inner-workings such as dependency injection, object composition specifications, auto-called methods and parameter definitions.

Although this is most certainly a good start, once I looked at it from the side of using the Symfony3 blog demo application as the groundwork for my own blog, it became clear that the many shortcomings prevent this sample project from being practical.

Although that is not necessarily its objectives, I believe that building an application (in this case, a blog) from this already solid codebase (which is well documented also) will not only help users learn, update , or perfect their web application development skills, but will, overall, provide a well-founded, convention over configuration approach. We will create more advanced features and custom functionality (hopefully) by maintaining Symfony3 best practices (not to mention PSR specification standards, except PSR-7).

So I’ve set out to add some additional functionality to the demo app in an attempt to make it more practical and eventually usable in a real world environment. This has already been implemented by someone who I enjoy reading, Matthias Noback, on his blog. We are going to be adding one feature at a time (all the current features at the end of each part will be updated in the Github repository of this project).

This first part of the Symfony3 Blog Demo REMIX series serves as an introduction to what the demo app has to offer and now we will get into which components and features we will be adding In this series…

Additional Features & Components To Be Added:

  1. A Category entity that will be attached to each Post entity including its repository, the creation of a CategoryController to manage them, its rendering inside the view templates for both front and backend, and its CRUD operations on the database via a standard Symfony Form implementation.
  2. A new Tag entity (including all the same types of components which we will make for the Category entity listed above) that can be attached to both Posts and Categories in a many-to-many relationship.
  3. Integration with a new, easier way to auto-wire your services and dependencies that will allow for framework agnostic classes that will act as a full replacement of the Symfony controller system and console command system by introducing Actions as controllers in the form of invokable methods. More details here.
  4. PSR-7
  5. Adding in a few helpful utilities and commands for the console component that will help to minimize the tediousness of repetitive tasks over the course of our app’s lifetime.

Complete series


This tutorial is originally posted by the author on his blog. This version has been edited for clarity.

Discover and read more posts from Jesse Griffin
get started
post commentsBe the first to share your opinion
Show more replies