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?
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"
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/
Related
I'm a long time Zend Framework user (now renamed Laminas). But I decided to give a try to last Symfony version. So I just installed it in 5.1.2.
I'm facing a question regarding the multiple environments deployments. In my compay, we have :
Local environment which is developer pc.
Development.
Staging.
Production.
In ZF-Laminas, we have a global.php file which is located in config directory.
For those of you who are not familiar with this framework, you can override key set in global.php file by creating local.php file.
In this global file, I use to put standard configuration for my application.
For example (prod) :
'open_id' => [
'client_id' => 1234
]
Then, I have development and staging files which car override those values for every environmenet. During the deployment, the file corresponding to the environment is copied to local.php.
Let's say staging.local.php.dist becomes local.php with :
'open_id' => [
'client_id' => 5678
]
Which is fine because value is overriding the one from global file.
I would like the same behavior in Symfony but I don't see something similar in Symfony 5.
So far, I only found two possibilities
Create a bundle which will allow me to have a <bundle_name>.yaml file in config/packages directory. According to the documentation (https://symfony.com/doc/current/configuration.html#configuration-files), I will be able to have dev, prod and staging overrides. But it forces me to create a bundle to handle just some standard configurations, which is huge.
Use .env files. But .env files only allow string data, not complex data like arrays.
What do I miss ? Or is it my "zend" way of doing things that is wrong ?
Thanks.
You can also create services_%env%.yaml (services_dev.yaml, services_test.yaml) files for each environment. It will allow you to define different parameters and override/define services for each environment.
Example:
config/services_dev.yaml
parameters:
hello: 'world'
From what I understand from your post, your goal is to have different config values based on the server you are on. If this is the case, you can use environment variables (in the .env file or .env.local for server specific config). You can then use these values in your applications by binding the env var to a parameter. This parameter will then be available within the configuration by using %parameter_name% as value or within the container. You can also pas parameters to services (service definitions are handled the same way as any other config). For more information you can checkout these sources:
https://symfony.com/doc/current/configuration/env_var_processors.html
https://symfony.com/doc/current/configuration.html
My goal is to add to a new Symfony 4.4 project an extra config file to define some behavior of the system. It could be anything, like, pancakes.yaml:
pancakes:
enablePancakes: false
I wish to know how can I load that config file. find a way to read its parameters and values to change some custom behavior the system might have but honestly I think I'm not smart enough to understand what the documentation says.
For now it could be anything, like printing the configuration file values, for now I only need to know how to load it.
you can update the following file :
# config/service.yaml
# This file is the entry point to configure your own services.
# Files in the packages/ subdirectory configure your dependencies.
parameters:
locale: 'en'
chat_update_interval: 10000
and use service decoration in your new application to override your parameters.
On my local development I face the issue that my applicaton uses relative path, to the domain, e.g. /css/style.css.
This works generally good, as I can configure virutal hosts on my develop machine, e.g. localhost.foo, so that the relative path is resolved to localhost.foo/css/style.css.
In my current situation, I cannot edit the hosts file to setup a development domain, so that the relative path to the domain does not work anymore. My develop url looks like localhost/projectfoo/public.
So that the app works, it should reference to localhost/projectfoo/public/css/style.css. Based on the relative path in the code it now reference to locahost/css/style.css, and there it will - obviously - not find the requested files.
Is there a way to configure laravel, to use at one case localhost/projectfoo/public as URL and on the other case the standard?
You can always add a new entry in your config files, lets say in the config/app.php
// this is the one that comes with laravel
'url' => 'http://localhost',
// this is the one that you can define
'url_public' => 'http://localhost/something_else/public'
and then use it in your view like:
<link href="{{ config('app.url_public') }}/style/style.css" rel="stylesheet">
Laravel has a helper function asset which generates a full URL for a relative path, based on your config app.url value.
For example, if app.url is http://localhost:
In your view put {{ asset("css/style.css") }} and Laravel will convert this to http://localhost/css/style.css
So in your case all you need to do is change app.url to http://localhost/public and then start using that asset method in your views instead of the relative path.
Better yet, add a new environment variable to your .env file (make sure to put it in .env.example as well) called APP_URL, e.g.
APP_URL=http://localhost
Then in config/app.php set it to:
'url' => env('APP_URL'),
That way you only have to set it once per environment
I want to access the base_path (base_url registered) of a specific Asset component directory from a controller in order to store my reports to a specific path preconfigured in config.yml.
I started changing my configuration, after upgrading to Symfony 2.7, like the following:
app/config/config.yml
framework:
assets:
version: 'v5'
version_format: '%%s?version=%%s'
base_path: ~
base_urls: ['http://cdn.example.com', 'https://secure.example.com']
packages:
reports:
base_path: bundles/mybundle
So, when I request a specific route, with the correct request parameters my controller generate the HTML from a particular Twig template and, at the end, it will be converted to PDF using KnpSnappyBundle.
At last, my purpose is to build a list of generated PDF reports accessible from a public assets directory.
$kernel->locateResource()
However, I can access the complete path using a workaround like the following:
$this->container->get('kernel')->locateResource('#MyBundle/Resources/public/reports')
Using parameters.yml
I have also asked for some hints and it seems legit to use the parameters.yml in order to manage the Asset component configuration. So, from the controller, they would be accessed using $this->getParameter() and, at the same time, as a configuration value for Asset.
The simplest way to deal with that is to define it as a parameter in parameters.yml, as you suggested yourself.
It's really easy to get it and it totally makes sense.
Update
I wanted to provide a bit more reasoning for my answer, so I will cite http://symfony.com/doc/current/best_practices/configuration.html as a reference.
Reading there, it seems that you should put into "parameters.yml" all infrastructure parameters which do not really change your application behaviours. I think this applies to your case as well: your application does not change its behaviour according to assets paths, it only needs to know where they are.
So, again, I'd say that putting them in parameters.yml not only provides you an easy solution but also it's a "good practice".
Is there a way of getting the Doctrine Uploadable extension to store a path relative to a directory in the database?
We use Capistrano to manage releases on our servers, so when a file is uploaded, the stored path looks like: /var/www/sitename/releases/20140625151300/web/uploads/$filename. While the files themselves are safe (uploads is a symlink), when the release is deleted the paths necome broken.
For example, the stored path could just be the filename or relative to %kernel.root_dir%.
I had the same problem and rather than dig into the listener (which I assume would be the other possibility) I set the path in my parameters file and then referenced that parameter in the stof_doctrine_extensions section. This way it allowed me to have the real path but allow it to be different for each version.
In app/config/parameters.yml
parameters:
....
acme.upload.path: '/The/Absolute/Path/app/Resources/files/uploads'
....
In app/config/config.yml
stof_doctrine_extensions:
....
uploadable:
default_file_path: %acme.upload.path%
....
If you're not using the stof bundle then I assume you would just pass the parameter into your listener as one of the calls.
Like I say though, I'm pretty sure you would be able to go into the listener and play with that but I found this the easiest approach.