VisioN
VisioN

Reputation: 145478

Address web path in config.yml in Symfony2

When configuring one bundle in Symfony2, I needed to set up a static path to CSS file from web folder, i.e. a line from config.yml:

content_css: "%path_to_web%/bundles/mybundle/css/styles.css"

%kernel.root_dir% returns the absolute root server path, but what in this case is the way of getting a virtual path to web folder? Is there any special variable for that or do I need to hard code that path?

Upvotes: 1

Views: 7214

Answers (2)

Emii Khaos
Emii Khaos

Reputation: 10085

You need no extra variable. The web root is defined with your webserver config. That's the way you configure the content_css option.

If you can reach your app.php (or app_dev.php) simple via http://www.example.com/app.php, then all assets are simply with the path reachable

content_css: "/bundles/mybundle/css/styles.css"

If you have exposed the whole symfony directory (strictly not recommended) and your app.php is reachable under the http://www.example.com/web/app.php, then simply prefix the path with /web.

content_css: "/web/bundles/mybundle/css/styles.css"

EDIT: Or you use a parameter in the parameters.yml. If you read this and you store your source in git or other (strongly recommended), then you have a paramaters.yml.dist with defaults, and every system (every developer or production server) has his own parameters.yml. Then add a parameter to yours and to the prods (and also to the .dist with some default):

parameters:
    # [...] some other parameters
    my_web_root: "/myproject/web"

the option looks like

content_css: "%my_web_root%/bundles/mybundle/css/styles.css"

Upvotes: 4

Wouter J
Wouter J

Reputation: 41954

As the directory structure of Symfony is defined in the Standard Edition and not the Symfony2 framework, there is no special parameter to use.

%kernel.root_dir% is defined by using __DIR__ in the AppKernel class. That's the one that should be used as the base path, you can do something like: %kernel.root_dir%/../web/

Upvotes: 4

Related Questions