How can I access the "web" folder from config.yml in symfony2

How can I access the web folder from config.yml in a symfony2 application? I tried% web%,% base_dir%,% asset_dir% and am in a brick wall.

+4
source share
3 answers

You can access resources in your web directory using the twig asset () function.

<script src="{{ asset('bundles/yourbundle/js/jquery-1.7.2.min.js') }}" type="text/javascript"></script> 

See http://symfony.com/doc/current/book/templating.html#linking-to-assets for more details.

I think you need to avoid setting local paths in the configuration / parameters file, try to keep everything as dynamic as possible.

+1
source

In config.yml you can use %kernel.root_dir%/../web .

If you want a convenient shortcut, in parameters.ini you can define something like:

 web_dir = %kernel.root_dir%/../web 

Then in config.yml you will use %web_dir% .

+14
source

In Symfony 3.3, we introduce many changes and simplifications to prepare us for the exciting release of Symfony 4.0 in November 2017.

Some of these changes are technically minor, but will have a profound effect in your applications. In Symfony applications, the well-known getRootDir () method of the Kernel class and its analogue to the kernel.root_dir parameter are misleading.

They return the path in which the application kernel is stored (usually AppKernel.php). In Symfony 2 and 3, this is usually the app / directory, so expressions like% kernel.root_dir% / .. / var / or% kernel.root_dir% / .. / web / are usually used. In Symfony 4, the kernel class was moved to the src / directory, so previous expressions will not be broken.

However, given that in most cases, getRootDir () is used to get the project root directory, in Symfony 3.3 we decided to add a new method to the Kernel class called getProjectDir (), which will give you exactly that.

This new method finds the root directory of the project by looking for the first directory containing the composer.json file, starting with the directory in which the kernel is stored and continues until the composite is found. json

In practice, this means that your application can simplify most or all of the expressions that use% kernel.root_dir%. For example: use% kernel.project_dir% / web / instead of% kernel.root_dir% / .. / web /.

From: New in Symfony 3.3: Easier way to get the project root directory

0
source

All Articles