Twig CRUD - Delete
In this video we will finish up our basic CRUD application with Symfony 3 and Twig by implementing the 'D' -
The logic for this action is incredibly simple - we don't even need a view.
In short we will grab the
BlogPost from the passed in
id, and remove it. Simple.
BlogPost has been removed, we will redirect the user (via a browser redirect / page reload) to the
We'll add in a touch of error checking by saying if the passed in
id doesn't exist then redirect the user to the
list view all the same. Not the best, and we could certainly add in a nice Flash Message to give the user a little heads-up as to what the heck just happened. But for the moment we aren't worrying about the visuals, just the 'raw' flow.
Out of all the actions, the
deleteAction is the closest to the way the API implementation will work. The difference will be around what happens with redirects versus simply returning status codes.
Wrapping Up The Twig Implementation
I've intentionally kept these videos on the short side as these processes are doing, largely, what frameworks like Symfony aim to make easiest out-of-the-box.
The really nice part is that re-writing these methods in their RESTful API equivalents is - dare I say - equally as quick. Once you have been shown ;)
The hard part, in my opinion, is figuring all this out for yourself, the first time you need to do it. After you have a working baseline, you can always refer back to that and do the old copy/paste.
There is a little more that we will come back and add in - pagination, filtering, and sorting. Again, for the moment, we are keeping things as basic as possible.
In the very next video we will switch to creating our CRUD setup using Symfony 3 and FOSRESTBundle to create our API.