Deleting Resources
This section is deprecated. However, as of now, the Sylius E-Commerce project is still resorting to this configuration so you might want to check it out.
Deleting a resource is simple.
Calling an Action with DELETE method
Currently browsers do not support the "DELETE" http method. Fortunately, Symfony has a very useful feature. You can make a POST call with parameter override, which will force the framework to treat the request as the specified method.
On submit, the delete action with the method DELETE, will remove and flush the resource. Then, by default it redirects to app_book_index
to display the books index, but just like for the other actions - it's customizable.
Overriding the Criteria
By default, the deleteAction will look for the resource by id. However, you can easily change that. For example, if you want to delete a book that belongs to a particular genre, not only by its id.
There are no magic hacks behind that, it simply takes parameters from request and builds the criteria array for the findOneBy
repository method.
Custom Redirect After Success
By default the controller will redirect to the "index" route after successful action. To change that, use the following configuration.
Custom Event Name
By default, there are two events dispatched during resource deletion, one before removing, the other after successful removal. The pattern is always the same - {applicationName}.{resourceName}.pre/post_delete
. However, you can customize the last part of the event, to provide your own action name.
This way, you can listen to app.book.pre_customer_delete
and app.book.post_customer_delete
events. It's especially useful, when you use ResourceController:deleteAction
in more than one route.
Configuration Reference
Remember that you can use controller's Fully Qualified Class Name (App\Controller\BookController
) instead of id app.controller.book
Last updated