Where can I add assets in symfony 5? - symfony

I'm using symfony 5 and I have no idea where can I pu my css, js, images files.
I read many posts about it but none helped me...
I tried:
composer require symfony/asset
symfony console assets:install
Here is the way i am using asset function in my twig files:
<link rel="stylesheet" type="text/css" href="{{ asset('css/base.css') }}">
And yes I have a file at public/css/base.css...
But when I inspect the result page, my line is replaced by:
<link rel="stylesheet" type="text/css" href="/css/base.css">
Did someone have any idea ?
Best regards,

If you're NOT using webpack (or webpack encore), your assets should be in your public folder which is the root of your public project. For example, if you have a file called style.css in public/css/style.css, you can access it in your browser with the url (assuming you use a local server started in the public dir) http://localhost:8000/css/style.css.
The Symfony asset component goal is to guess if your project is in a subdirectory and therefore to resolve the correct path. In order to use it in a Twig template with the previous imaginary file :
<link rel="stylesheet" href="{{ asset('css/style.css') }}">
The asset function will be able to produce the right path for your file.
If you're using webpack, the setup is a bit harder so if you don't I won't explain it here.

Related

How to use mix function in laravel

I'm writing a program using laravel. To manage css and js, I used versioning and call it with a function like this
<link rel="stylesheet" href="{{ mix('/css/app.css') }}">
The code above produces html output as follows
<link rel="stylesheet" href="/css/app.css?id=53a226b301fc510ddf79">
When I upload to hosting, why does the above code only produce html like this ?
<link rel="stylesheet" href="/css/app.css"> (the mix-manifest.json / id file not load)
this is my localhost
and this is on the hosting
Did you run npm run dev after deploy it on your hosting?
Make sure in your webpack.mix.js have .version() method.
I think in your localhost your laravel run in development mode. And in the hosting the laravel run with production mode. Check your .env file.

Symfony doesnt find css file

I've just done a fresh install of the latest Symfony version. Inside my templates folder I've created a new folder called website which contains a base.html.twig and a home.html.twig.
File structure: templates/website/base.html.twig
Inside my public directory I've created the following css/app.css. Now in my base I've done:
<link rel="stylesheet" href="{{ asset('css/app.css') }}" type="text/css">
Inside my stylesheet I'm just setting the body background colour to red so I know it works. For whatever reason it isn't loading my css. When I view source it appears as:
<link rel="stylesheet" href="/css/app.css" type="text/css">
and clicking the href I just get a Symfony 404. I cannot figure out why it isn't loading my static CSS file.
I don't want to use Symfony Encore as thats overcomplicating things for my project. I have also tried assets:install command and nothing installed or changed.
UPDATE:
I installed and done a basic setup with Symfony Encore in the hope I could at least get some CSS working. Still get the exact same issue.
If you want to use the asset funtion you have to install the component first with the following command:
composer require symfony/asset
That is really all there is to it if you do not wish to use webpack encore.
Optional: If you happen to use PHPStorm with the Symfony plugin, go to settings > symfony and change Web Directory from app to public for the new directory that Symfony 4 uses. This gives you all the autocomplete goodness and your references will work.
You should store your static files inside the assets directory at the root of your project. So just place the css-folder with the app.css-file in <root>/assets/css/app.css
Source: https://symfony.com/doc/current/best_practices/web-assets.html

How can I install assets in web/css/ folder

I have installed Symfony 2.8 and I have put header.css in web/css, but I don't know how can I access it.
The web browser returns the following error:
GET http://www.grupoinversores.com/css/header.css 404 (Not Found)
You need symlink. For example... here you js, css, img directories:
AppBundle/Resources/public/
In console command $ php app/console assets:install --symlink creates symlink for this js, css, img directories for AppBundle/Resources/public/.
Now you js, css, img here: web/bundles/app/css, web/bundles/app/js, web/bundles/app/img and for currect generate path use this:
<link rel="stylesheet" href="{{ asset('bundles/app/css/style.css') }}">
http://symfony.com/blog/new-in-symfony-2-6-smarter-assets-install-command
it seems based on live page as I looked there you have created a "header.css" directory not a file. No 404, though.
Do you use Twig? Can you post the part of your HTML that tries to call your header.css file?
If you are using Twig, have you tried something like:
<link href="{{ asset('cms-assets/css/bootstrap/bootstrap.css') }}" rel="stylesheet" />
inside your HTML?
In my case the bootstrap.css file is inside the folder:
/web/cms-assets/css/bootstrap
And as seen, I can access it using the "asset" with Twig

Webapp does not load images

I'm developing a webapp on Eclipse and this is the file system directory structure:
myapp
- src
- build
- WebContent
- pages
- css
- images
- modules
- META-INF
- WEB-INF
When I run this webapp with Tomcat (Run from Eclipse) I can load in the browser
the jsp pages contained in (myapp -> WebContent -> pages) using for example this URL:
http://hostname:8080/myapp/pages/somepage.jsp
However the problem is that the loading of the css and images in such somepage.jsp fail.
In the code of the somepage.jsp I pointed the css in this way:
<link href="../css/new_style.css" rel="stylesheet" type="text/css"/>
And the image in this way:
<img src="../images/someimage.png"/>
Problem is I have no clue why such images and css are not loaded.
The problem is in the relative path, always a safer approach to do something like
<link href="<%=request.getContextPath()%>/css/new_style.css" rel="stylesheet" type="text/css"/>
or a JSTL equivalent
<link href="${pageContext.request.contextPath}/css/new_style.css" rel="stylesheet" type="text/css"/>
than you're starting from the root, accounting for context path as well
Better way is to use ${pageContext.request.contextPath} as it returns the name of the application's context.
For ex,
myapp/css/new_style.css can be dyanmically formed as {pageContext.request.contextPath}myapp/css/new_style.css
Have a look here to know What does "./" (dot slash) refer to in terms of an HTML file path location?

Symfony 2 - having troubles with asset function

I'm having troubles with calling stylesheets and javascript via asset() function..
my app is running on dev enviorment so the url is - dproc.local/app_dev.php/
The base.css is in /src/Dproc/Resources/views/Dproc/css/base.css
<link href="{{ asset('/css/base.css') }}" type="text/css" rel="stylesheet" />
Doesnt work.
How should my link have to be? I didnt understood how asset() function works..
Thanks
You need to run the command
asset install --web
so that the asset files are copied to web folder.

Resources