A quick post concerning a problem I got when I started to move this blog to a symfony application. By the way, there’s still no release date planned yet considering the time I have for it. But let’s go back to this problem.
In order to optimize at most my application, key word of the new release, I wanted to put my CSS, Javascript and picture files in numerous subdomains (you could find many articles about this subject on Thomas Parisot’ blog. I’m definitly too lazy to have symfony’s core files in each project so it was impossible for me to edit the AssetHelper.php (the file in which use_javascript(), image_tag() and use_stylesheet() functions are defined). The other problem is that since it’s not a class, it was impossible to overload it. Though, this is almost what I’ve done.
Required
For this exemple, I’ll use symfony’s default directories and the following subdomains:
- http://css.blog/
- http://js.blog/
- http://images.blog/
I won’t explain here the way the set them up since it’s not the goal of this post, just make them work!
Creating custom helpers
To start, we will create our custom helpers which will come to simply “overload” the existing ones in order limit the amount of lines of code and do no disturb the working code. We will create a file named “customHelper.php” in the ./lib/helpers directory so we could use it in our backend as well. We then, add the following code :
-
< ?php
-
-
function custom_image_tag($img, $params = array())
-
{
-
if (!$img{strlen($img) - 4} != '.')
-
{
-
$img .= '.png';
-
}
-
return image_tag(sfConfig::get('app_custom_img_dir', 'http://images.blog/') . $img, $params);
-
}
-
-
function custom_use_javascript($js, $position = '', $options = array())
-
{
-
return use_javascript(sfConfig::get('app_custom_js_dir', 'http://js.blog/') . $js, $position, $options);
-
}
-
-
function custom_use_stylesheet($css, $position = '', $options = array())
-
{
-
if (!$css{strlen($css) - 4} != '.')
-
{
-
$css .= '.css';
-
}
-
-
return use_stylesheet(sfConfig::get('app_custom_css_dir', 'http://css.blog/') . $css, $position = '', $options = array());
-
}
No comment, the code is very simple… There’s a limitation for CSS files and pictures which doesn’t have necessarily an extension and which is not added correctly. It can result in a problem for server request. Javascript files are correctly loaded though…
In our exemple, I did use on purpose calls to sfConfig() to simplify migration to the production server later but, as you can guess, I haven’t defined it which explain the default url !
Finally, don’t forget to edit your css files if it links pictures in it.
Usage
Before starting coding, we have to add our “custom” in the “standard_helper” line in our ./apps/frontend/config/settings.yml before doing an old
-
symfony cc
The syntax remains perfectly indentical to the existing one except the need to prefix our functions' call with "custom_". Example: custom_image_tag() instead of image_tag(). To change to subdomains adresses easily, don't forget to add the following lines in your ./apps/frontend/config/app.yml
custom_css_dir: http://css.exemple.com/ custom_js_dir: http://js.exemple.com/ custom_img_dir: http://img.exemple.com/
Good night day !
Related posts:
Je pense que le plugin sfDynamicsPlugin devrait te plaire. Il ne propose pas l’utilisation de sous-domaines mais change toute la gestion des fichiers CSS et JS dans un projet.