response - How to set status code from service layer in Symfony 2? -


i have service helping me validate forms. want set http status code inside of when finds form invalid.

is ok so? how do it? thanks!

the way (probably there's lot more solutions) throwing (custom) exception in service in case form invalid.

then create exception listener listen on kernel exception event. configuration of such listener (services.yml)

kernel.listener.your_listener:     class: acme\your\namespace\yourexceptionlistener     tags:         - { name: kernel.event_listener, event: kernel.exception, method: onexception }  

and onexception method in yourexceptionlistener class:

public function onexception(getresponseforexceptionevent $event) {     $exception = $event->getexception();     if ($exception instanceof yourcustomexceptioninterface) {         $response = new response('', 400);         $event->setresponse($response);     } } 

this way nicely decoupled stuff, , form validation service independent of response.


Comments

Popular posts from this blog

php - render data via PDO::FETCH_FUNC vs loop -

javascript - Which segment of a polyline was clicked? -

c# - Avoiding duplicate methods for Task and Task<T> -