Symfony 2 - having troubles with asset function - symfony

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.

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.

Where can I add assets in symfony 5?

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.

Django, forcing reload of css/js, and collectstatic

Let's say that I've done a good bit of update to my css files on my dev machine (with my browser set to ignore the cache, no always seeing updates right away).
In this question and other places, I've seen the approach of adding a version number to the links:
<link type="text/css" href={% static "/path/mystyles.css?version=2" %} rel="stylesheet">,
which is fine, but on the dev machine, I get an error in the console, and the file isn't found:
GET http://127.0.0.1:8000/path/mystyles.css%3Fversion%3D2 net::ERR_ABORTED 404 (Not Found)
I've read that perhaps collectstatic will fix it on the dev side, but there's an ominous-looking warning:
You have requested to collect static files at the destination
location as specified in your settings:
/patttthhhhh/static
This will overwrite existing files!
Are you sure you want to do this?
What will happen when I run collectstatic? Where will it get files from to overwrite those?
The recommended way would be to use something like django-compressor (usage) or the likes to process your CSS into a single file and then bake a hash into the filename or dir. So that way your asset would become /mystyles.beb23fe.css.
What ended up working easily was changing the format of the version tag:
<link type="text/css" href="{% static '/path/mystyle.css' %}?version=2" rel="stylesheet">
instead of:
<link type="text/css" href={% static "/path/mystyles.css?version=2" %} rel="stylesheet">
Is fine in dev and production.

Symfony assets on localhost (Wamp)

I am running symfony on my localhost for learn, i have configured my localhost folder http://localhost/symfony to http:/symfonyweb.
Everything works fine but style can't loading,
<link rel="stylesheet" type="text/css" href="/assets/css/style.css">
full url is: http://symfonyweb/assets/css/style.css
Your asset should be load using the function asset() which is linked to /web root directory.
In your case, your assets should be like <link rel="stylesheet" type="text/css" href="{{ asset('assets/css/style.css') }}">

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

Resources