Symfony 2 and static resources - symfony

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.

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

Where to put the LICENSE file in a Symfony application?

I have a web application made with Symfony2 and I want to add my license choice in the repository. The thing is that the default Symfony2 project has a LICENSE file in the root directory and I don't know where I am supposed to put my LICENSE file and if I should delete the Symfony2's one. Also happens with README file.
I guess that you Symfony2 programmers sure know how to handle this.
Your own application code should be located mostly in the src folder. If you take a look at some Bundles or other components of Symfony2 you can see that they all have a separate LICENSE file in their bundle root directory. So the correct choice for your file would be:
src/.../.../LICENSE
In this case your LICENSE file would also be included if you install your bundle via composer on a different project. In that case it would be at:
vendor/.../.../LICENSE

Install assets from app directory

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!

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