I'm using Symfony3 and I want to know how can I add a photo to my website or a css file
Can anyone give me a detailed solution because I saw some people using the asset and they put their photos in \public\images and I didn't understand how it really works knowing that I don't have the folder \public\images in Resources ?
The best way is to use Assetic to manage your assets (css, js, images). You can store them in the directory app/Resources/public and dump them before deploying to the production environment with the command php bin/console assetic:dump --env=prod --no-debug.
Thank you I found a solution by myself
I store the images in src/nomDuBundle/Resources/public then I executed the command php bin/console assets:install.
A folder named Bundle will be created in /web , I found there my Bundle , I took the absolute PATH & in the twig file I used this code :
<img src="{{ asset('bundles/nomBundle/images/votreImage.png') }}" />
It works for those who have a problem with this
AppBundle is recommended by the Assetic documentation to centralize all ll that we need to include in the HTML header.
Is it why a new symfony generation with symfony phar always create a AppBundle ? In any case it is likely.
src/AppBundle directory is just like the ring of the Lord of the Rings, all power of front office is centered there.
In it Create a Resources directory and : images, css, js,... sub-directories.
src/AppBundle
|
-- Resources
|
--public
|
-- images
|
-- css
|
-- js
|
-- ....
install assetic with composer
configure config.yml
configure Appkernel :
class AppKernel extends Kernel{
public function registerBundles(){
$bundles = [
...
new Symfony\Bundle\AsseticBundle\AsseticBundle(),
...
]
...
in twig views :
{% image '#AppBundle/Resources/public/images/example.jpg' %}
<img src="{{ asset_url }}" alt="Example" />
{% endimage %}
command line :
$ php bin/console assetic:dump --env=prod --no-debug
Command line generates directories and image files with automatic names. And it works well.
web
|
-- images
|
-- css
|
-- js
|
-- ....
Related
When I run the php artisan optimize command with the --force option, my application instance doesn't work at http://localhost/project/public/invoice/create.
However, it does work at http://localhost:8000/invoice/create when I run php artisan serve.
Why isn't http://localhost/project/public/invoice/create working? How can I rollback?
The Laravel file structure is set up:
public
|-- css
| -- app.css
|-- js
| -- app.js
| -- some-other.js
|-- index.php
These files referenced as follows:
http://localhost/css/app.css
http://localhost/js/app.js
http://localhost/index.php OR http://localhost/
The /public directory is the base and contains the index.php file, which is the entry point for all requests entering your application and configures autoloading. This directory also houses your assets such as images, JavaScript, and CSS.
I've got a problem with assetic and the path of my images in CSS on Symfony.
My base template, my base CSS, and the images used in this CSS are in the folder app/Resources.
I have this:
app/
Resources/
public/
css/
base.css
images/
mybackground.png
views/
base.html.twig
In the file base.css, I want to load the file mybackground.css as a background, so I have this line:
background-image: url('../images/mybackground.png');
And in the file 'base.html.twig', I load the css with:
{% stylesheets '#base_css' filter='cssrewrite' %}
<link rel="stylesheet" type="text/css" charset="utf-8" media="all" href="{{ asset_url }}" />
{% endstylesheets %}
I also tried without the filter cssrewrite.
In app/config/config.yml, I have this:
assetic:
filters:
cssrewrite: ~
assets:
base_css:
inputs:
- '../app/Resources/public/css/base.css'
Before try, I used all of these commands:
php app/console cache:clear
php app/console cache:clear --env=prod
php app/console assetic:dump
php app/console assetic:dump --env=prod --no-debug
php app/console assets:install --symlink web
As you can guess, my background image isn't loaded (the browser returns a 404 not found error).
I searched a lot on internet, and I found similar cases but with the resources in some bundles not in app/Resources.
When you create a public directory in the Resources directory of your bundle, and run the assets:install --symlink command, a symbolic link is created in the web/bundles directory, corresponding to the name of your bundle.
Also, to retrieve your image, you should use the relative path of the web directory instead of the absolute path of your asset, which is :
/bundles/bundlename/images/background.jpg
EDIT
If you really doesn't want to place them inside a bundle, just move them from the app/Resources directly to the web folder, and call it in your css using :
url('/images/background.jpg');
See symfony2 how to access app/Resources/img from browser?
after calling assets:install and assetic:dump you will find your files in the web-directory (webservers document root) all files NOT in /web are not accesible by httpd so just take a look at your /web folder and look inside the bundles folder
so the relative path would be
/bundles/bundlename/images/background.jpg
this you can use in your css file
I know this has been asked a few times before but I'm still having trouble getting my CSS background images to work with assetic.
I've read Path of assets in CSS files in Symfony 2 and all my background images are set to ../images in my CSS:
background: url(../images/icons/icon.png)
And in my twig files as:
{% stylesheets 'bundles/commondirty/css_original/c.css' filter="cssrewrite" %}
<link href="{{ asset_url }}" rel="stylesheet" type="text/css" />
{% endstylesheets %}
I've tried clearing all my cache stuff with:
php app/console cache:clear --env=prod --no-debug
rm -rf app/cache/*
rm -rf app/logs/*
Then as stated in the above answer:
php app/console --env=prod assets:install web
php app/console assetic:dump --env=prod
This works locally ('app.php', 'app_dev.php', and '/'), but as soon as I upload the files to production it fails to load background images.
Both local and production CSS is as follows so I don't understand why it doesn't work?
url("../../bundles/main/images/icons/lrg_coin.png")
Permissions? Do I need to reload/clear something?
Edit
cached CSS files on dev and prod are pointing to:
url(../../bundles/main/images/icons/lrg_coin.png) when I'm guessing it needs to be ../ not ../../? How can this be changed?
Ok, I figured what I was doing wrong so thought I'd post for future reference. I had use_controller set to false in my prod_config.yml which was overriding my config.yml settings (which dev_config.yml uses)
assetic:
use_controller: false
Changing this to true and clearing/reloading everything as stated in my question solved the issue. I knew it was something simple.
This is my architecture folders :
I would like to load an picture using asset() in a twig template which is located in
sites/sf2.local/views/index.html.twig
and the picture is in
sites/sf2.local/assets/img/logo_om.gif
So in my config.yml I have added this line :
assetic:
read_from: %kernel.root_dir%/../sites/sf2.local/assets/
but it doesn't work, I can't load the picture using :
<img src="{{ asset('img/logo_om.gif') }}" alt="img" />
in the twig template..
How can I do this ?
First, I think you have mistaken assets for assetic. Twig function asset does nothing more than making you assets absolute. It does not affect where those assets are read from. That is described in this docs:
Linking to assets
On the other hand, assetic describes where compiled (and optionally filtered, merged, uglified, etc...) resources are read from. That only applies to JS and CSS.
Possible solution:
Open Terminal, navigate to your web symlink that directory:
cd /.../sf2/web
ln -s ../sites/sf2.local/assets/img
ln -s ../sites/sf2.local/assets/js
ln -s ../sites/sf2.local/assets/css
Now you have 3 additional symlinks (not copies) in your web and using asset('img/some-pic.png') actually targets the symlinked img directory.
Is this symlinking acceptable?
Alternative solution (whole assets):
cd /.../sf2/web
ln -s ../sites/sf2.local/assets
and use it:
asset('assets/img/some-pic.png')
I include my CSS with the following code:
{% stylesheets 'bundles/majorproductionssewingdivasite/css/*.css' filter='cssrewrite' %}
<link rel="stylesheet" type="text/css" href="{{ asset_url }}" />
{% endstylesheets %}
In dev, this allows me to use image sprites without any problems. The resulting URL to my sprite is:
http://localhost/diva/web/bundles/majorproductionssewingdivasite/images/diva-sprites.jpg
But, in prod, it gets mapped to:
http://localhost/diva/bundles/majorproductionssewingdivasite/images/diva-sprites.jpg
Notice the lack of web directory.
The generated code in the CSS file is as it should be, and all my CSS (both the dev 'chunks' and the finalized prod assetic dump) are at web/css/. Any ideas as to why the prod environment is skipping the web directory?
EDIT: what's weird is that both dev and prod generate the same URL in the CSS itself:
url('../../bundles/majorproductionssewingdivasite/images/diva-sprites.jpg')
Solution is to dump the assets in the prod environment:
$ app/console assetic:dump --env=prod
Remember to Clear the Cache
php app/console cache:clear --env=prod --no-debug
berore
php app/console assetic:dump --env=prod --no-debug
Also remember to appropriate configure assetic, in your config_prod.yml set as below:
assetic:
use_controller: false
Edit:
As said in Symfony docs (actually in chapter refers to dev environment): tell Symfony to stop trying to process these files dynamically
In debug mode, Assetic 1.1 also seems to rewrite CSS even when it's not in the {% stylesheets %} tag. So it's worth checking that's there when you turn off debug in production.