Note that there are some explanatory texts on larger screens.

plurals
  1. POPath of assets in CSS files in Symfony 2
    primarykey
    data
    text
    <h3>Problem</h3> <p>I have a <a href="http://en.wikipedia.org/wiki/Cascading_Style_Sheets">CSS</a> file with some paths in it (for images, fonts, etc.. <code>url(..)</code>).</p> <p>My path structure is like this:</p> <pre><code>... +-src/ | +-MyCompany/ | +-MyBundle/ | +-Resources/ | +-assets/ | +-css/ | +-stylesheets... +-web/ | +-images/ | +-images... ... </code></pre> <p>I want to reference my images in the stylesheet. </p> <h3>First Solution</h3> <p>I changed all paths in the CSS file to absolute paths. This is no solution, as the application should (and has to!) be working in a subdirectory, too.</p> <h3>Second Solution</h3> <p>Use Assetic with <code>filter="cssrewrite"</code>.</p> <p>So I changed all my paths in my CSS file to </p> <pre><code>url("../../../../../../web/images/myimage.png") </code></pre> <p>to represent the actual path from my resources directory to the <code>/web/images</code> directory. This does not work, since cssrewrite produces the following code:</p> <pre><code>url("../../Resources/assets/") </code></pre> <p>which is obviously the wrong path.</p> <p>After <code>assetic:dump</code> this path is created, which is still wrong:</p> <pre><code>url("../../../web/images/myimage.png") </code></pre> <p>The twig code of Assetic:</p> <pre><code>{% stylesheets '@MyCompanyMyBundle/Resources/assets/css/*.css' filter="cssrewrite" %} &lt;link rel="stylesheet" href="{{ asset_url }}" /&gt; {% endstylesheets %} </code></pre> <h3>Current (Third) Solution</h3> <p>Since all CSS files end up in <code>/web/css/stylexyz.css</code>, I changed all paths in the CSS file to be relative:</p> <pre><code>url("../images/myimage.png") </code></pre> <p>This (bad) solution works, except in the <code>dev</code> environment: The CSS path is <code>/app_dev.php/css/stylexyz.css</code> and therefore the image path resulting from this is <code>/app_dev.php/images/myimage.png</code>, which results in a <code>NotFoundHttpException</code>.</p> <p>Is there a better and working solution?</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload