I try to add some config in config/* directory.
I'll use a very simple example, to illustrate the problem, but keep in mind that this problem occur for every .yaml config files for any services. I am not able to make inheritance between config files.
So in services.yaml I have this code :
#config/services.yaml
parameters:
app.debug: 0
smtp.user: ''
app.locale: '%kernel.default_locale%'
app.product.number_per_page: 20
container.dumper.inline_factories: true
services:
# default configuration for services in *this* file
_defaults:
autowire: true # Automatically injects dependencies in your services.
autoconfigure: true # Automatically registers your services as commands, event subscribers, etc.
bind:
$locale: '%kernel.default_locale%'
$product_per_page: '%app.product.number_per_page%'
And then i try to access $product_per_page into my controller :
#src/Controller/myController.php
public function listProduct(int $product_per_page){
dump($product_per_page);
die();
}
When I execute this code, it give me the following errror :
Could not resolve argument $product_per_page of
"App\Controller\ProductController::listproduct()", maybe you forgot to
register the controller as a service or missed tagging it with the
"controller.service_arguments"?
So I have to copy this config files (config/services.yaml), and then paste it into config/services_dev.yaml, services_prod.yaml, services_test.yaml
And then it is working, but this is not suitable at all...
Because I have to do that for every config files... So i copy and paste in every environnement each time I update ANY configuration.
Of course, I have try to cache:clear and composer update, but nothing do.
I was expecting Symfony read the file as explain here but it is not working for me.
I have try to create another Symfony project, and now it is working in this one. But I want to understand what is wrong ? Is there a thing I have done which have result into a bug that cannot being correct even ater updating Symfony, even after having update composer, even after having clear the cache.
I can't believe there's nothing to do but to create a new Symfony project in which you copy the files from the old one.
Related
I am implementing the interface autowiring.
The config/services.yaml example works just fine but when using config/services.php instead of config/services.yaml the code in config/services.php does not get triggered.
So this works:
# config/services.yaml
services:
# ...
App\Util\Rot13Transformer: ~
# the ``App\Util\Rot13Transformer`` service will be injected when
# an ``App\Util\TransformerInterface`` type-hint is detected
App\Util\TransformerInterface: '#App\Util\Rot13Transformer'
But this does not:
// config/services.php
namespace Symfony\Component\DependencyInjection\Loader\Configurator;
use App\Util\Rot13Transformer;
use App\Util\TransformerInterface;
return function(ContainerConfigurator $configurator) {
$services = $configurator->services();
$services->set(Rot13Transformer::class);
// the ``App\Util\Rot13Transformer`` service will be injected when
// an ``App\Util\TransformerInterface`` type-hint is detected
$services->alias(TransformerInterface::class, Rot13Transformer::class);
};
Is this a bug or am I missing something?
I also added invalid php code into config/services.php and no error is thrown. So this leads me to conclude that config/services.php is not being ran at all.
Just for a bit of context, starting in 5.1 Symfony has been moving towards using php based config/services.php and routes.php files for configuration in place of yaml files.
However, you need to remove the corresponding yaml file in order to allow the php files to be loaded. You can see this in Kernel::configureContainer().
And in my setups at least, I also find that I have to manually run "bin/console clear:cache" after making changes to either services.php or routes.php. The cache is not being automatically refreshed like it does with the yaml files. Could just be something I am doing wrong.
The php based service and route builders are quite powerful. They give you lots of syntax checking and autocompletion help. Well worth investigating. The docs have side by side examples of yaml vs xml vs php.
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.
I am trying to create application in Symfony 2, to learn as much as I can, as beginner. First AppBundle which was created by default was easy. Second I created using create:bundle "ResultBundle", was a bit rough and I got "ClassNotFoundException" at first, but after some settings it worked again as expected.
Armed with not so deserved confidence, I tried to create third bundle "ClinicBundle" and then everything fell apart. Although I created routing.yml for new bundle, and set it correctly in AppKernel.php (link goes right to new class ClinicBundle.php), and set app/config/routing.yml correctly (same as for previous ResultBundle) and added the bundle to composer.json autoload, same as in previous bundle, I kept getting "ClassNotFoundException" for this ClinicBundle, like it can't be loaded in AppKernel.php though when I point to link in that row:
new ClinicBundle\ClinicBundle()
It goes smoothly to the right file. Every advice I have found online was already done and set correctly. Everything was in place but "ClassNotFoundException" kept on and it just wasn't working. I tried clear:cache also but same error popped up even then. I checked everything and it was all in place. routing files, paths, composer autoload. AppKernel...
Finally, I followed some ill advice from a forum and run some dump-autoload command which "generated autoload files" and everything after is much much worse. Now, I am getting long long FileLoaderLoadException error with many lines and main is this one:
Cannot load resource "#ClinicBundle/Resources/config/routing.yml". Make sure the "ClinicBundle/Resources/config/routing.yml" bundle is correctly registered and loaded in the application kernel class. If the bundle is registered, make sure the bundle path "#ClinicBundle/Resources/config/routing.yml" is not empty.
I have no idea what is wrong since bundle is registered in AppKernel and this routing file is not empty, it's like this:
clinic_homepage:
path: /clinic
defaults: { _controller: ClinicBundle:Default:index }
app:
resource: '#ClinicBundle/Controller/'
type: annotation
The same structure as previous ResultBundle which worked before all this happened. Also, the ResultBundle does not work anymore too, if I put it on top of routing.yml file in app/config same error happens but for ResultBundle. This is how app/config/rounting.yml file looks like:
clinic:
resource: "#ClinicBundle/Resources/config/routing.yml"
type: annotation
result:
resource: "#ResultBundle/Resources/config/routing.yml"
type: annotation
app:
resource: '#AppBundle/Controller/'
type: annotation
What happened? Does anyone know what is missing here? How do you add new bundle so that it does not report "ClassNotFound"? Thanks!
Just without type: annotation
clinic:
prefix: /some-prefix
resource: '#ClinicBundle/Resources/config/routing.yml'
I hope I have phrased that correctly, but I am trying to install vendors into my symfony2 project, which I have just downloaded from SVN, but I keep getting this error,
[Symfony\Component\Config\Exception\FileLoaderLoadException]
Cannot import resource "parameters.ini" from "/Users/john/Sites/trunk/awesome/app/zone/config/../../config/config.yml".
[InvalidArgumentException]
The file "parameters.ini" does not exist (in: /Users/john/Sites/trunk/awesome/app/zone/config/../../config, ).
Script Sensio\Bundle\DistributionBundle\Composer\ScriptHandler::clearCache handling the post-install-cmd event terminated with an exception
[RuntimeException]
An error occurred when executing the "'cache:clear --no-warmup'" command.
Obviously it seems to be looking in the wrong directory, but I am not sure what file is pointing to those files.
Also, I am quite new to Symfony, so I am unsure of where to look to start fixing the problem...
I suppose that you have something like this into app/config/config.yml
imports:
- { resource: parameters.ini }
And you haven't parameters.ini file
The parameters.ini file is used into Symfony 2.0 and now, in Symfony 2.1 (if you use composer,I suppose that you use Symfony 2.1) this file in turned into parameters.yml so please check for this "error" and try again
I'm guessing parameters.ini file wasn't put into your repository as it stores sensitive data (like database password).
You should create it locally. Some people put template files into the repository, so look for files named like parameters.ini.dist. If there's no such file grab the original file from symfony standard edition and adapt it to your environment.
As others pointed out, Symfony 2.1 uses parameters.yml instead of parameters.ini by default. You can still use ini file if you want though. If you have a parameters.yml file or want to start using it, follow instructions given in other answers.
I fixed this by deleting the vendor directory and ran composer install again.
I could not manage to find a way to include different security.yml files that would be included depending on Symfony2`s environment. For example I wanted to have an in-memory user provider for my acceptance tests, cause I don't really need to test my entities and stuff here, I only want to make an acceptance test for my views.
But, as it turned out, it's not an easy thing to do. I removed security.yml from includes in my config.yml, renamed it to security_prod.yml and created a security_test.yml which has the in_memory user provider. Then I've included security_prod.yml and security_test.yml in my production and testing configs respectively.
Yet it does not seem to work at all:
$ SYMFONY_ENV=test app/console cache:clear
[Symfony\Component\Config\Definition\Exception\InvalidConfigurationException]
You are not allowed to define new elements for path "security.providers". Please define all elements for this path in one config file.
$ SYMFONY_ENV=prod app/console cache:clear
[Symfony\Component\Config\Definition\Exception\ForbiddenOverwriteException]
Configuration path "security.access_control" cannot be overwritten. You have to define all options for this path, and any of its sub-paths in one
configuration section.
It appeared to me like the security.yml filename was hardcoded (which would be way too weird for Symfony), and it wasn't.
So the question is: how do I get multiple security.ymls with Symfony? And what could be causing this behaviour?
Instruction for those, who is looking for it (and do not red comments):
Create different config files for different environments: config_test.yml, config_dev.yml, config_prod.yml
Create different security files: security_test.yml, security_dev.yml, security_prod.yml
Import security_test.yml in config_test.yml and so on for other environments. Example for config_test.yml:
imports:
- { resource: security_test.yml }
Make sure you included security_*.yml only once (basically author did this mistake)