How to include .css and .js files in a php file (index.html.php for example) on Symfony? (I'm using Symfony2 if that matters), I read that I must include the src/link within an asset function like follow:
<link href="<?php echo $view['assets']->getUrl('MyStyle.css') ?>" rel="stylesheet" type="text/css" />
<link href="<?php echo $view['assets']->getUrl('buttons.css') ?>" rel="stylesheet" type="text/css" />
Neither that nor the ordinary of php, worked with me.
Update: css files locate on \Bundle\Resources\public\css
You have two options:
If your assets are in src/Your/Bundle/Resources/public/
, you have to call ./app/console assets:install --symlink web/
to create a symlink from web/bundle/yourbundle
to src/Your/Bundle/Resources/public/
so that they are accessible from web (alternatively you could do ./app/console assets:install web/
to copy the files directly, without symlink.
Then you could do this:
<link href="<?php echo $view['assets']->getUrl('/bundle/yourbundle/css/MyStyle.css') ?>" rel="stylesheet" type="text/css" />
With assetic you don't need to install/copy the files, assetic will do it for you:
<?php foreach ($view['assetic']->stylesheets(
array('@YourBundle/css/MyStyle.css')) as $url): ?>
<script type="text/javascript" src="<?php echo $view->escape($url) ?>"></script>
<?php endforeach; ?>
See http://symfony.com/doc/current/cookbook/assetic/asset_management.html (click on the PHP tab on the code samples).