ducin
ducin

Reputation: 26437

symfony2 assets management in bundles

Currently I'm having one global assets.yml file in my project, where I define my assets and I use them throughout the whole application. I have several bundles and all their assets are defined in this global assets.yml which is not clear and not good.

I'd like to define one assets.yml with corresponding assets per each bundle. They would lie in the bundle configuration.

I don't know how to make them accessible for the entire application. Should I use import somehow or does the framework load files (which name follow certain convention)? I'd appreciate hints how can I achieve the above.


edit: I should explain a bit more... In my config.yml I import assets.yml as a resource:

imports:
    - { resource: assets.yml } 

and the assets.yml looks like the following:

assetic:

  use_controller: false

  filters:
    cssrewrite: ~
    yui_js:
      jar: %kernel.root_dir%/Resources/java/yui-compressor.jar
      #apply_to: "\.js$"
    yui_css:
      jar: %kernel.root_dir%/Resources/java/yui-compressor.jar
      #apply_to: "\.css$"

  assets:   
    jquery:
      inputs:
        - '%kernel.root_dir%/Resources/public/js/jquery-1.8.0.js'
        - '%kernel.root_dir%/Resources/public/js/jquery.sizes.js'
        - '%kernel.root_dir%/Resources/public/js/jquery.form.js'
        - '%kernel.root_dir%/Resources/public/js/jquery.validate.js'
        - '%kernel.root_dir%/Resources/public/js/jquery.metadata.js'
    [...]

Upvotes: 1

Views: 661

Answers (1)

Vadim Ashikhman
Vadim Ashikhman

Reputation: 10126

Check the official documentation for How to expose a Semantic Configuration for a Bundle

Some refs:

When you create a bundle, you have two choices on how to handle configuration:

  1. Normal Service Configuration (easy): You can specify your services in a configuration file (e.g. services.yml) that lives in your bundle and then import it from your main application configuration. This is really easy, quick and totally effective. If you make use of parameters, then you still have the flexibility to customize your bundle from your application configuration. See "Importing Configuration with imports" for more details.

  2. Exposing Semantic Configuration (advanced): This is the way configuration is done with the core bundles (as described above). The basic idea is that, instead of having the user override individual parameters, you let the user configure just a few, specifically created options. As the bundle developer, you then parse through that configuration and load services inside an "Extension" class. With this method, you won't need to import any configuration resources from your main application configuration: the Extension class can handle all of this.

So the main idea is to create Extension class in each bundle and load bundle specific configuration files.

You can also check core symfony or third-party bundles for some examples.

Upvotes: 2

Related Questions