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.
Related
According to README.md from https://github.com/hslavich/OneloginSamlBundle/tree/1.x
The only way to provide certificate for OneloginSaml is via app/config/config.yml
We strongly don't want to load them by pasting it into yaml file, instead, we want to load certificates from a file, given the path to the file in this yaml file.
I noticed that the only way to achieve this would be to override the class HslavichOneloginSamlExtension.
What is the "right" way to override a HslavichOneloginSamlExtension class to allow certificates to be loaded from a file path?
The would suggest using environment variables for private certificates and use them in .yml files likes this:
privateKey: '%env(PRIVATE_KEY)%'
I think it is not possible to load a file in a yml config.
I would like to inject a YAML configuration to a service in Symfony. Currently, I inject path of my bundle and a relative path to the file and parse it manually with Yaml::parse but is there an easier way to do this?
So the goal would be that the constructor of the service would receive an array of the already parsed YAML config.
To put some more detail about it:
Currently, I have a YAML file in a Resources directory in my bundle and to parse it in a service, I use something like this:
$yamlFile = sprintf(
'%s/%s',
$container->get('kernel')->getBundle('MyBundle')->getPath(),
$pathToFile
);
So I have to inject both the container (can I somehow inject just the path to the bundle?) and the path to the file. I wouldn't mind restructuring my system a bit, e.g. put the yaml file in the config directory of the bundle, if that would help, but I'd like to keep it separate from other configuration.
You can load needed configuration in bundle extension like described there: http://symfony.com/doc/current/cookbook/bundles/extension.html and save it to new parameter, or inject to any service you like.
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".
I have the Laravel Administrator Plugin and I set up a setting administrator page that is located in:
app/config/administrator/settings/site.php
The application can store the data, but when i try to get some data in one of my controllers like this:
Config::get('administrator.settings.site')
I can get a returned value... Always null or array 0.
How I can get those configuration values?
Solution:
You can use a file path rather than dot notation for file paths:
Config::get('administrator/settings/site.variable_name');
Some Explanation
Dot notation is used to get variables inside the config array within a file, rather than to denote file paths.
Therefore you can use Config::get('administrator/settings/site.variable_name'); to get $variable_name from administrator/settings/site.php, or ENV_DIR_NAME/administrator/settings/site.php depending on your environment.
Directories within in app/config are read as environments. For example app/development/database.php will work with Config::get('database.whatever') when you're in your "development" environment while app/production/database.php will be read with Config::get('database.whatever') when you're in the "production" environment.
Laravel falls back to the config/config/*.php file when no environment-specific file is present.
Note
I believe that admin package has a config file in app/config/packages/frozennode/administrator/administrator.php which the package would like you to use for it's settings. That should be available using this convention: Config::get('package::file.option');
I ran into a very similar problem when using this Laravel-Excel package.
The solution I needed was slightly different to the solution above. I needed to take advantage of Laravel's config overriding feature to have one config setting for normal execution and a different one for testing. Specifically, I normally wanted Excel files to be stored in storage/excel and I wanted them to be put in storage/testing/excel under testing.
The technique of using a slashed path didn't work because it points to a specific file so didn't respect the different environments:
$directory = Config::get('packages/maatwebsite/excel/export.store.path');
The solution was to use a package prefix so that the config loader would respect the environment:
$directory = Config::get('excel::export.store.path');
I'm not exactly sure where the excel shorthand name comes from but I suspect it's something to do with the Excel facade in the package exposing itself as 'excel'.
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/