symfony - ResponseListener changes Content-Type of assets -


in dev environment after adding response listener content type of css files switched text/css text/html. goal add caching headers responses of symfony application.

service definition:

response_listener:     class: appbundle\eventlisteners\responselistener     tags:         - { name: kernel.event_listener, event: kernel.response } 

service class:

<?php  namespace appbundle\eventlisteners;  use symfony\component\httpkernel\event\filterresponseevent;  class responselistener {     public function onkernelresponse(filterresponseevent $event)     {         // while testing nothing special here         $response = $event->getresponse();         $event->setresponse($response);     } } 

assetic config:

assetic:     debug:          "%kernel.debug%"     use_controller: true     bundles:        [ appbundle ]     filters:         cssrewrite: ~ 

twig tag:

{% stylesheets     'bundles/app/css/login.css'     filter='cssrewrite' %}     <link rel="stylesheet" type="text/css" href="{{ asset_url }}"/> {% endstylesheets %} 

with text/html content type css not rendered. removing service definition makes work correctly again. in prod environment works or without response listener , js files no problem @ all.

any ideas?

i had similar issue , although didn't went trace on core bundles of symfony, think due fact that, on development instance, assetic use controller render css.

i stopped looking @ moment found, that, you, config_dev.yml stated :

assetic:     use_controller: true 


figured out when try play response object in symfony, requested format gets somehow lost while listening on response. symfony's own responselistener seems set text/html defaullt because there no content type defined.

the fix quite easy though :
in class responselistener, our listening function become

public function onkernelresponse(filterresponseevent $event) {     $response = $event->getresponse();      // params of route     $route_params = $event->getrequest()->get('_route_params');      // , figure out if should in specific format     if(isset($route_params['_format'])) {          // getmimetype function on request object come in handy job         $format = $event->getrequest()->getmimetype($route_params['_format']);          // reinstate right content type         $response->headers->set('content-type', $format);     }     $event->setresponse($response); } 

while never have issue in production environnement state it, because assets dumped command line assetic tool, not controller.

and actually, if in responselistener

die($event->getrequest()->get('_controller'));  

and navigate css file direct url see display :

assetic.controller:render

which totally validate fact css gets constructed on fly render view of assetic bundle controller when have configuration stated above.


Comments

Popular posts from this blog

node.js - Using Node without global install -

How to access a php class file from PHPFox framework into javascript code written in simple HTML file? -

java - Null response to php query in android, even though php works properly -