change the flag in the configuration file
- vlkodlak
- Member | 167
Hi
after nette update, I have problem into config.neon with sending parameter to the procedure.
into my config file
services:
- App\Model\sysValueRepository(
values: %parameters%
)
and after work with this parametr, but now nette say me %parameters% is deprecated, use @container::getParameters()
but I am absolutly without idea how insert into config.neon this data to %parameters%
Last edited by vlkodlak (2024-07-10 00:48)
- vlkodlak
- Member | 167
uestla wrote:
@vlkodlak Just replace
%parameters%
with@container::getParameters()
as the message suggests.
yes, I tried that first, but … if it was that simple, you wouldn't be writing here.
the sent field does not contain any values. I need to pass an array of values read from common.neon
- m.brecher
- Generous Backer | 837
@vlkodlak
I need to pass an array of values read from common.neon
Iam almost 100% sure that method DI\Container::getParameters() doesnt supply parameters defined in the section parameters in config.neon file. It supplies other parameters defined in Bootstrap.
If you have your own parameters in config.neon like this:
config.neon
parameters:
first: 'anyValue'
second: 'otherValue'
You should pass them to your service one by one, not all together like an array:
services:
- App\Model\sysValueRepository(%first%, %second%)
or create your own array collection of your parameters and pass them together as an array this way:
parameters:
myParameters:
first: 'anyValue'
second: 'otherValue'
services:
- App\Model\sysValueRepository(%myParameters%) # pass all parameters as an array
Last edited by m.brecher (2024-07-10 17:01)
- Marek Bartoš
- Nette Blogger | 1244
Iam almost 100% sure that method DI\Container::getParameters() doesnt supply parameters defined in the section parameters in config.neon file. It supplies other parameters defined in Bootstrap.
It does, they are the same parameters. Bootstrap just has priority over config files and overrides parameters from config.
- m.brecher
- Generous Backer | 837
@MarekBartoš
It does, they are the same parameters.
Yes, you are right !! But still the whole subject of config static/dynamic parameters defined in config.neon files or in Bootstrap is not easy understand at all.
Static parameter defined in Bootstrap:
$configurator->addStaticParameters([
'parameter' => 'value',
]);
You can pass this parameter in config.neon file into some service the same way as parameter defined in config.neon file in section parameters:
parameters:
parameter: 'value'
These definitions are identical (thanks for important explanation). They can be used in definitions of services like this:
services:
- App\MyService(%parameter%)
But it is not possible to get them the serviceLocator way using DI\Container::getParameter() method:
$parameter = $container->getParameter('parameter'); // exception
The above code in presenter produces an exception:
Nette\InvalidStateException
Parameter 'parameter' not found. Check if 'di › export › parameters' is enabled.
Do you know why DI\Container::getParameter() not works ?? Is it above mentioned wrong configuration of parameter di export ??
Last edited by m.brecher (2024-07-10 22:50)
- m.brecher
- Generous Backer | 837
@MarekBartoš
di > export > parameters – solved
I have changed in configuration setting of di > export > parameters to yes
di:
export:
parameters: yes
tags: no
and method DI\Container::getParameters() delivers correctly all static parameters.
If parameters export is disabled method DI\Container::getParameters() returns empty array, which is little misleading, would be better if throws exception -no config parameters can never occur!! The other method DI\Container::getParameter($name) returns exception with message which forwards the right way to the solution. However, if one tries the first method, one may make the mistake like I did that these are different parameters than in config.neon.
Would be nice to add the exception to DI\Container::getParameters() when returns empty array + add some information about DI\Container::getParameters() to documentation.
Last edited by m.brecher (2024-07-10 23:00)
- Marek Bartoš
- Nette Blogger | 1244
Just pass specific parameters to the service via %parameter.name% syntax, don't turn export on. They are not in DIC for a reason – you can't be sure in runtime whether parameter is available, better have it checked in compile time.
Only using %parameters% to pass all parameters is deprecated, because you likely have no reason to do that