I am following a tutorial for symphony 2 but cant get the style sheet to load all I get is a 404.
I have tried also adding it into the route but I get a permission error on the style sheet.
app/Resources/views/base.html.twig
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html"; charset=utf-8" />
<title>{% block title %}symblog{% endblock %} - symblog</title>
{% block stylesheets %}
<link href="{{ asset('css/screen.css') }}" type="text/css" rel="stylesheet" />
{% endblock %}
I have added the style sheet into app/Resources/views/web/css/screen.css
Any ideas why this is not working? Tutorial
You put css files in a wrong directory.
It should be web/css/screen.css instead of app/Resources/views/web/css/screen.css
I also suggest you to use assetic instead of manually put css and js to the web directory. These articles might help (take a look at the assets:install command):
How to Use Assetic for Asset Management
Linking to Assets
Including Stylesheets and JavaScripts in Twig
Projects assets (resources from your app/ folder) are ignored by assets:install command. Only resources from bundles will be copied into the web/ folder automatically.
You should place your app (project) resources directly in the web/ folder.
http://symfony.com/doc/current/cookbook/assetic/asset_management.html
By the way: ONLY the web/ folder is public.
Look at the rendered source code of your html in the browser and you will see that your css file is prefixed with a web/. This is what the asset() command does
Related
I have symfony 4.1 installed via composer and the asset component.
I have a css file at assets/css/dashboard.css and in my templates/base.html.twig I included this:
{% block stylesheets %}
<link href="{{ asset('css/dashboard.css') }}" rel="stylesheet" />
{% endblock %}
I haven't modified anything, but somehow the template is not getting called.
I tried many variations of the path, adding slashes, dots thinking maybe the path is wrong but nothing.
The css file has no issue, I dumped it's contents and pasted it inside my template's <style></style> tags and it works.
I don't know what is going on
For short:
The asset() function points on your public folder.
So in your example, your dashboard.css should be in public/css/dashboard.css than this <link href="{{ asset('css/dashboard.css') }}" rel="stylesheet" /> should work.
The longer explanation:
Usually you will structure your scripts, styles and images in your assets folder. But in production you don´t need good readable css/less/scss/js code and so you want to minify it.
And the minified (uglified) code should be copied to your public folder.
So you want to use Webpack Encore to minify your code and "deploy" it to your projects public folder.
In symfony´s documentation you can find a simple Example how to use Webpack Encore.
Why the assets are going to the public folder?
During the security concept of symfony (and the most other frameworks) the public folder is the only accessible folder. So everything the browser have to read goes there. In case of symfony your styles, scripts, images and so on.
Im learning Symfony and trying to set up a boilerplate app in Symfony 4
This Symfony document describes how to include assets in your page, namely using the asset package like so..
<img src="{{ asset('images/logo.png') }}" alt="Symfony!" />
<link href="{{ asset('css/blog.css') }}" rel="stylesheet" />
I have installed this package and trying to link to a css file in my /public directory.
I know Symfony recommends placing assets in an /assets folder at the root, but I would like to avoid this if possible. It makes more sense to me to place assets in the public directory.
My Twig base template is as follows:
<html>
<head>
<meta charset="UTF-8">
<title>{% block title %}Welcome!{% endblock %}</title>
{% block stylesheets %}
<link href="{{ asset('public/css/main.css') }}" rel="stylesheet" />
{% endblock %}
</head>
<body>
{% block body %}{% endblock %}
{% block javascripts %}{% endblock %}
</body>
<footer>footer</footer>
</html>
Problem
When I load the route/page/template, the linked css is rendered as a simple link from the root (as if Im linking it as <link href="public/css/main.css" rel="stylesheet" /> - there is of course no route set for public/* and this returns 404/Not Found.
Its as if the asset() function is not even used.
Ive tried
Restarting my local Symfony server
moving the asset to an assets folder at the root at adjusting the link accordingly
verifying that the asset package was installed (it is)
googling this issue (nothing for Symfony 4)
Questions
How can I let Symfony know that the public/* path is a filesystem path to assets, NOT a URL route, and include my assets successfully?
Is there such a feature to set a default location for assets other than the recommended /assets folder at the root?
Paths in the asset function should be relative to your public directory, not the project root.
So, pretending your server's docroot is public_html, and an asset is in public_html/css/main.css, the asset call would be {{ asset('css/main.css') }}.
Additional note: The asset function doesn't give you the ability to put assets outside the public directory. They must be in the public directory.
Firstly maybe you have to add the asset functionality to Symfony:
composer require symfony/asset
Then you should to use in Twig temlate the relative path to your public directory:
<link href="{{ asset('css/style.css') }}" rel="stylesheet" />
for the public/css/style.css style file.
BTW it is also works for Symfony 5.
Check if webpack is watching files ... (yarn watch) After hours of searching for an answer one of my project team members asked if I had that running ={ Worked immediately after that.
I have a question about symfony2 assetic. The next situation in project:
We got a themes of project, themes must be stored in one folder. Now it's in:
/
/src
/Dwd
/FrontendBundle
/Resources
/themes
/default
/css
reset.css
style.css
/img
img.png
layout.html.twig
/not_default
/css
reset.css
style.css
/img
img.png
layout.html.twig
and i have the problem of including the styles and images from styles (cssrewrite) filter from theme
In layout.html.twig the next code:
<html>
<head>
{% stylesheets 'bundles/dwdfrontendbundle/css/*' filter="cssrewrite" %}
<link rel="stylesheet" href="{{ asset_url }}" />
{% endstylesheets %}
</head>
<body>
</body>
</html>
And when I do "php app/console assets:install" assetic don't install styles from Dwd/FrontendBundle/Resources/themes...
So my question is How i can add this path or include this css files in layout.html.twig of any file?
p.s. We extend this layout in /views templates trough adding include path to twig.loader service, like {% extends 'layout.html.twig' %}.
For some reason I can't load the css files.
Here is the structure of the bundle that I use
BD
WebsiteBundle
public
css
And here is how I try to load the css files
{% stylesheets 'bundles/bdwebsite/css/*' filter='cssrewrite' %}
<link rel="stylesheet" href="{{ asset_url }}" />
{% endstylesheets %}
I did assetic:dump
What I'm doing wrong?
You referred to bundles/bdwebsite/css/* in your stylesheets tag.
This way assetic looks for all css files in web/bundles/bdwebsite/css (a folder that currently does not exist) and not in src/BD/WebsiteBundle/public/css.
In order to have your files in the right place before executing assetic:dump...
... use app/console assets:install web which will copy them to the web folder...
... or my recommendation app/console assets:install web --symlink for symlinks.
This is a bit frustrating. According to Symfony best practices, a bundle's web assets (images, css, js) should be placed in src/vendor/path/to/bundle/Resources/public. Running app/console assets:install copies the contents of that public folder to web/bundles/bundlename. In the official documentation, Twig templates are shown to grab these assets with code like:
{% block stylesheets %}
<link rel="stylesheet" type="text/css" href="{{ asset('/css/styles.css') }}
{% endblock %}
When I try it, instead of magically grabbing my assets from web/bundles/bundlename/css/styles.css, it instead just goes for web/css/styles.css. Is this expected behavior? The official documentation is less than clear about this.
In order to try to combat this issue, I tried embracing assetic's ability to dynamically serve assets. I tried:
{% stylesheets '#mybundle/Resources/public/css/*' %}
<link rel="stylesheet" type="text/css" href="{{ asset_url }}" />
{% endstylesheets %}
But got the following exception:
An exception has been thrown during the compilation of a template ("You must add mybundle to the assetic.bundle config to use the {% stylesheets %} tag in mybundle:Home:index.html.twig.") in "/home/kevin/www/src/mybundle/Resources/views/Home/index.html.twig
The official Symfony documentation makes it appear that it should work out of the box with no configuration necessary.
So, TLDR:
Am I not understanding how assets should be loaded?
How do I address the assetic exception?
To fix the assetic exception you need to configure your bundle in your config.yml like this:
assetic:
bundles: [ MyAwesomeBundle ]
The {{ asset(...) }} twig function will serve files relative from your web root.
You use app/console assets:install to install static assets within your web root and you can later easily point to them using the asset function like this {{ asset('/mybundle/css/site.css') }}