If you want to create a css, js, image like assets in a symfony application and accessing them in twig template then follow the example below.


Example structure


Assume that you've created 'site.css', 'site.js' and 'site.png' in your bundle and want to access them in your twig templates.


app
...
...
src
MyBundle
Resources
public
css
site.css
js
site.js
images
site.png
...
...
web
bundles
mybundle
css # Empty
js # Empty
images # Empty
...
...

Create assets


As you can see above, your files are not under web->bundles->mybundle directory yet. Run command below to create symlinks to the files. For more info, please read assets:install command. After running the command, you'll see the copies of files in web->bundles->mybundle directory.


# make a hard copy of the assets in web/
$ php app/console assets:install

# if possible, make absolute symlinks in web/ if not, make a hard copy
$ php app/console assets:install --symlink

# if possible, make relative symlinks in web/ if not, make a hard copy
$ php app/console assets:install --symlink --relative

Example usage


...img src="{{ asset('bundles/mybundle/image/site.png') }}" ...
...link rel="stylesheet" href="{{ asset_url }}" ...