Install assets from app directory - symfony

Here is my directory structure
/app/Resources/public
/app/Resources/public/css
/app/Resources/public/iamges
/app/Resources/public/js
I need to install these assets to /web directory. What is the right way to do it?
I've tried app/console install:assets but it installs assets only from bundles. Is it possible to store global assets in app directory?

Your global assets should go directly into the web folder. There is no need to put them in the app folder and then copy them!
The install:assets or assetic:dump command are for assets which belong to one bundle only and which are kept inside this bundle to make them reusable.
What you can do though is to keep the assets which belong to your application in your "application bundle". Most of the time you got at least one bundle which is not reusable, representing your application core. I keep my base templates (base.html.twig) and the main assets in there so I can use assetic:dump on them!

Related

Symfony : Why put assets in web/ diectory instead of bundle's Resources/public/?

I always thought symfony is one of the best PHP Framework because it uses bundles : all files are grouped in the bundle folder : views, controller, assets, so I can create severals web sites with one symfony installation (like Wordpress Multi-site) ... But I found this in the official documentation :
Best Practice : Store your assets in the web/ directory.
It means files are no more grouped ! Can some one explain me what are the advantages ?
I also read Sensio will perhaps no longer use bundles !! Why ???
If the assets are bundle specific, you can use then under the bundle. web directory is the end-user facing root and referencing assets in an website / API from your bundle to the end-user is not suggested from security point of view.
So, even if you have your assets under Resources/public/ directory. You have to run the command asset:install to install those assets to web/bundles directory. This will help the bundle to make compatible with plug and play feature across the application.

How to keep a Symfony2 symlink when transfer to ftp

I have a Symfony2 project, and I was wondering how to use assetics which are located in the bundle folder in the view. So I searched the web to find a solution, and I found this command :
php app/console assets:install web
And it works fine on my computer. It created a symlink (seen as a shortcut on windows) so I can call my assets just with something like :
asset("bundles/mybundle/folder/file.css")
But everything changed when... I uploaded it.
The symlink became an actual folder with a copy of all the content of the public folder from my bundle. So when I made changes on src/my/bundle/resources/public/folder/file.css, it didn't change web/resources/bundles/mybundle/folder/file.css because it was a copy, and not a shortcut.
How can I keep a symlink ? Is it because I'm on windows and my server is on Linux ?
The symfony2 standard way is to have a copy of all js and css files in your web directory.
Think of it as a clean separation between user/client accessed files and strict server side files. You can workaround it, but you shouldn't.
To solve your problem, after each update to your server you have to delete all files in web/resources/bundles (to make sure removed files from your bundle are not present anymore) and rerun
php app/console assets:install web

Symfony 2 and static resources

I am new to Symfony 2 and there is something I would like to know..
It looks like you have to put your static resources to this location: MyBundle/Resources/public
but if I want to use twig functions like asset, it is good if resources are at this location: web/MyBundleName/
Why do I have to duplicate my resources ? (one in the src/MyBundle/Resources/public and the other one in web/MyBundle)
Run app/console assets:install web to copy resources from the activated bundles to the web/bundles folder.
This is not a duplication. The web/bundles folder is the place where all the assets from all the bundles are being installed to. The folder should be ignored by your VCS.
The reason for this approach is that 3rd party bundles — and the reusable bundles you'll create later — don't have access to the web folder of an application. Installing assets with the command solves this problem.

How can I make Assetic automatically move CSS background images from my Bundle to the Web directory?

After a great deal of hassle using Alexandre Salomé's [nonetheless excellent] "Sass, Compass, and Assetic in 10 minutes" I've finally gotten the Compass Assetic filter to convert my SCSS files into CSS files within the /web directory of my project. I even think I've got the compass image-url() function to reference images where they should be.
Unfortunately, I don't know how to keep images inside my bundle and have Assetic properly copy or rewrite them into the /web directory when needed. They just don't go there. For the time being (...and it seems like this may SOMEHOW be the intended functionality??) I'm just copying them into a /web/images directory. That can't be right. ...Right?
This is not something assetic can and should do, but something Symfony can do.
First of all, you should place the assets on the correct place in your bundle structure. These should be placed in the Resources/public directory. This is the place where stylesheets, scripts, images and all other public things lives. For more information about the bundle structure, please read the documentation: "How to use Best Practices for Structuring Bundles"
The FrameworkBundle comes with a usefull command called assets:install this will install all assets from all bundles into the web/bundles/<bundle_name> directory. This command is run every time you use a composer update or composer install command. When you update some assets in your bundle, you can run this command to copy everything to the web directory:
$ php app/console assets:install
Sometimes, you don't want to run this command every time you update someting. For instance, if you begin styling your pages, you will need to run that command every minute. If you server supports using symlinks, you can use the --symlink option. This way, you don't need to run that command everytime.

Symfony 2 - Working with assets

I need some tips on how to work with assets in Symfony 2. For example, do we have to always perform the assets:update every time an image is added ? I know Assetic take care of the management on css and javascript files but what about images? What would be the best practice for the front-end development with Symfony 2 ? How do you guys setup your css, images and js files in your app to make it easy to develop, deploy and change ?
Regarding images, if you added it into your public folder, I think there's no need to perform assets:update
However, if you add the image within the resources folders of a bundle, you might have to, depending on your OS and which options you used when called assets:install
If you're using an OS which supports symlinks (linux, OS X, and I guess all OS but Windows), you can install the assets calling (I don't exactly remember the call, the important thing here is the symlink option):
php app/console assets:install web --symlink
This way, instead of having a copy of each bundle's resources, you'll have a symlink, so there should be no need to update. If you have an OS which doesn't support symlinks, I think you'll have to keep updating or reinstalling assets (in fact, I always used assets:install, I didn't knew there was an update option :P).
Regarding the set up, I usually put all css, js, images and any public resources inside a bundle if it is used only within the bundle, and place it onto the public folder if it's used by many bundles, or I plan to use it in other bundles.
As of Symfony 2.7 this will generate relative symlinks in web directory:
php app/console assets:install web --symlink --relative
In composer.json add:
"extra": {
"symfony-assets-install": "relative"
}
This will also generate relative symlinks on composer update.
Here is cool think about --symlink.You can configure(config) one time and use forever.If you want more http://www.w3docs.com/snippets/symfony/how-to-keep-symlinks-in-web-bundles-after-composer-update.html

Resources