dependency on a non-existent parameter - symfony

I renamed a service from Notifications to Notification (Because I have already a class called Notifications), but when I tried to call it I have the following error:
ParameterNotFoundException in ParameterBag.php line 106:
The service "notification" has a dependency on a non-existent parameter "app.bundle.notification.class". Did you mean one of these: "app.bundle.utils.class", "app.bundle.notifications.class"?
In my Bundle/Services I have 2 files:
- Notification.php
- Utils.php
In Notification.php the class is called class Notification {...}
In my config file services.yml
notification:
class: %app.bundle.notification.class%
arguments: [#templating]
I don't know where it can found the suggest value app.bundle.notifications.class
I tried to clear the cache but I have the same error.

You have to declare your service by this way:
parameters:
your.service.class: ACME\YourBundle\Services\ServiceName
services:
your.service:
class: "%your.service.class%"
arguments: [#templating]
Hope it will help you :)

Symfony errors on container mistakes are - IMO - one of the best thing of this stack. Look closer:
Did you mean one of these: "app.bundle.utils.class", "app.bundle.notifications.class"?
Compiler tells you probable parameters you can use (and which exist). Check the ones of your bundle and rename this param to the new convention.
The simpliest thing to do: search whole project if it looks a bit hidden.
PS. Don't forget to delete cache.

Related

Symfony3 Docs WSSE fail "Cannot replace arguments if none have been configured yet"

Following this
http://symfony.com/doc/current/security/custom_authentication_provider.html
Results in
Service "security.authentication.provider.wsse.wsse_secured": Cannot replace arguments if none have been configured yet.
I cannot find anything about this error anywhere. This uses the doc's WSSE code and it fails.
This repo shows it failing https://github.com/jakenoble/wsse_test
I want to get it working eventually with the FOS User Bundle. But I cannot get it to work with a basic Symfony3 install so FOS User Bundle is out of the question at the moment.
Having dug around a bit...
There is a an arg at element index_0 on class Symfony\Component\DependencyInjection\ChildDefinition the object for the arg at element index_0 has an id of fos_user.user_provider.username_email.
The replace call then attempts to get the arguments of fos_user.user_provider.username_email, but there are none. Then the error occurs.
Any ideas?
TL;DR Move your service definitions below the autoloading definitions in services.yml and change the WsseFactory code to
$container
->setDefinition($providerId, new ChildDefinition(WsseProvider::class))
->setArgument('$userProvider', new Reference($userProvider))
;
Full explanation.
The first mistake in the supplied code is that the services definitions prepend the autoloading lines. The autoloading will just override the previous definitions and will cause the AuthenticationManagerInterface failure. Moving the definitions below will fix the issue. The other way to fix the issue is aliases as #yceruto and #gintko pointed.
But only that move will not make the code work despite on your answer. You probably didnt notice how changed something else to make it work.
The second issue, the failure of replaceArgument, is related to Symfony's order of the container compilation as was correctly supposed. The order is defined in the PassConfig class:
$this->optimizationPasses = array(array(
new ExtensionCompilerPass(),
new ResolveDefinitionTemplatesPass(),
...
$autowirePass = new AutowirePass(false),
...
));
I omitted the irrelevant passes. The security.authentication.provider.wsse.wsse_secured definition created by the WsseFactory is produced first. Then ResolveDefinitionTemplatesPass will take place, and will try to replace the arguments of the definition and raise the Cannot replace arguments exception you got:
foreach ($definition->getArguments() as $k => $v) {
if (is_numeric($k)) {
$def->addArgument($v);
} elseif (0 === strpos($k, 'index_')) {
$def->replaceArgument((int) substr($k, strlen('index_')), $v);
} else {
$def->setArgument($k, $v);
}
}
The issue will appear cause the pass will call Definition::replaceArgument for index_0. As the parent definition doesn't have an argument at position 0 neither in the former services.xml nor in the fixed one. AutowirePass wasn't executed yet, the autogenerated definitions has no arguments, the manual definition has the named $cachePool argument only.
So to fix the issue you could use rather:
->setArgument(0, new Reference($userProvider)); //proposed by #yceruto
->replaceArgument('$userProvider', new Reference($userProvider)); //proposed by #gintko
->setArgument('$userProvider', new Reference($userProvider)); // by me
All of them will entail the calls of Definition::addArgument or Definition::setArgument and will work out. There are only the little difference:
- setArgument(0, ... could not work for some other scenarios;
- I like ->setArgument('$userProvider' more than ->replaceArgument('$userProvider' due to semantics. Nothing to replace yet!
Hope the details why the issue appears now clear.
PS. There are also few other funny ways to overcome the issue.
Fix the config a bit:
AppBundle\Security\Authentication\Provider\WsseProvider:
arguments:
0: ''
$cachePool: '#cache.app'
public: false
Or set an alias of Symfony\Component\Security\Core\User\UserProviderInterface to let the autowire do the rest for you.
$container
->setDefinition($providerId, new ChildDefinition(WsseProvider::class))
// ->replaceArgument(0, new Reference($userProvider))
;
$container
->setAlias('Symfony\Component\Security\Core\User\UserProviderInterface',$userProvider)
;
This look like at this moment the provider definition doesn't have the autowired arguments ready, maybe related to the order in which the "CompilerPass" are processed, by now you can solve it with these little tweaks:
change this line in WsseFactory.php:
->replaceArgument(0, new Reference($userProvider))
by:
->setArgument(0, new Reference($userProvider))
and add this alias to services.yml to complete the autowired arguments of the new provider:
Symfony\Component\Security\Core\Authentication\AuthenticationManagerInterface: '#security.authentication.manager'
EDIT: (deleted the previous contents due to the comment)
I see you don't use FOSUserBundle that makes things more comples. I did not yet create PR.
I think you are missing custom user entity. (see the link I have provided to create your own entity - my PR would be similar to these lines, if you are unable to create your own entity using my description and the link I'll provide PR, but that will take longer).
The steps you have to take:
Create your own User entity via src/AppBundle/Entity/User.php
Create DB table via php bin/console doctrine:schema:update --force
Configure Security to load your Entity - use AppBundle\Entity\User;
Then you have to create your own user. This can be tricky see password encoding for more.
(optional) Forbid interactive users If you want to login using
username OR email you can create Custom Query to Load the User
These steps should be enough for you to create your own user Entity and you don't have to use the FOSUserBundle.
EDIT:
Ok, so read some source code, and in ChildDefinition:100 you can see, that arguments are also indexed by argument's $name. So, it must be, that at compile time arguments of autowired services are passed with their named indexes, instead of numbered indexes.
WsseFactory.php
->replaceArgument(0, new Reference($userProvider));
so argument should be referenced by it's name:
->replaceArgument('$userProvider', new Reference($userProvider));
After this change, you will get new exception error, which says that it's not possible to autowire $authenticationManager in WsseListener service by it's interface. You can fix it simply by specifying alias for that interface in your services.yml:
Symfony\Component\Security\Core\Authentication\AuthenticationManagerInterface: '#security.authentication.manager'
I guess this issue is related with new autowire feature, I will try to investigate it later why it is so.
For now, you can use old way to define services:
services.yml:
app.security.wsse_provider:
class: AppBundle\Security\Authentication\Provider\WsseProvider
arguments:
- ''
- '#cache.app'
public: false
app.security.wsse_listener:
class: AppBundle\Security\Firewall\WsseListener
arguments: ['#security.token_storage', '#security.authentication.manager']
public: false
in WsseFactory.php, following lines:
new ChildDefinition(WsseFactory::class);
new ChildDefinition(WsseListener::class);
translates into:
new ChildDefinition('app.security.wsse_provider');
new ChildDefinition('app.security.wsse_listener');
It seems the issue is because my config in services.yml came before the new auto wiring stuff.
So simply moving it below the auto wiring fixes it.

Symfony DI calling to static method

I use this library https://github.com/smsapi/smsapi-php-client to send sms from site. But I had a problem when I try to handle base class in service. So my question is there is the way to call a static method with with argument? I try to do it this by factory but this doesn't work for me. This is my code:
smsapi.client:
class: SMSApi\Client
factory: ['SMSApi\Client', createFromToken]
properties:
attributes: '%smsapi_token%'
But I got error below:
Warning: Missing argument 1 for SMSApi\Client::createFromToken(), called in ../cache/dev/appDevDebugProjectContainer.php on line 5073 and defined
As described in the doc: Passing Arguments to the Factory Method:
you can use the arguments options inside the service container.
As Example:
smsapi.client:
class: SMSApi\Client
factory: ['SMSApi\Client', createFromToken]
arguments:
attributes: '%smsapi_token%'
Hope this help
PS: please share the repo of the library you are using or the main part of the source code in order to handle the situation.

Getting Symfony base URL from a service?

I have a service that needs to access the current application base URL (what's returned by app.request.getBaseURL() in Twig views). Currently, my config is like this:
services:
WidgetModel:
class: AppBundle\Model\WidgetModel
scope: prototype
arguments: ['%widgets%']
So as a second argument, I would like to inject the base URL. Is it possible? If not, what would be proper solution?
As far as I know there is no builtin base url service. Which is actually a bit of a red flag that maybe having your component depending on it might not be such a good idea. But I can't think of a good reason why.
So normally, one would just inject the request object. But that has it's own problems as documented here: http://symfony.com/blog/new-in-symfony-2-4-the-request-stack
Instead, inject the #request_stack service and pull the url from it:
class WidgetModel
{
public __construct($widgets,$requestStack)
{
$this->baseUrl = $requestStack->getCurrentRequest()->getBaseUrl();
If you do find yourself needing the baseUrl in multiple services then you could define your own factory type service to generate it. But again, that might mean your design needs rethinking.
You can use the expression language in your service definition.
This example should do what you want:
services:
WidgetModel:
class: AppBundle\Model\WidgetModel
scope: prototype
arguments: [%widgets%, "#=service('request').getBaseUrl()"]
It fetches the request service and then executes the getBaseUrl method.
You will need to add a second parameter in your WigetModel for the base URL.
To complete the answer, in Symfony 3 you can use request_stack to get the base url using expressions languages (updated link) such as:
services:
WidgetModel:
class: AppBundle\Model\WidgetModel
scope: prototype
arguments: [%widgets%,"#=service('request_stack').getCurrentRequest().getBaseUrl()"

Symfony2 - use parameters on config.yml for the arguments of a service

I'm trying to use the method described on this other question and I wasn't able to make it work:
Symfony2: How to inject ALL parameters in a service?
In summary, I would like to define the arguments of my service in a unique file using a variable. I would then only pass this variable as an argument to my service. If one day, I decide to change the arguments, I don't have to change all the calls on my service. I would just change the content of this variable. However, I'm having the following error:
Symfony\Component\DependencyInjection\Exception\InvalidArgumentException You cannot dump a container with parameters that contain references to other services (reference to service "doctrine.orm.entity_manager" found in "/company.usermanagerservice/em").
Was anybody able to make it work? Or do you have other ideas?
Edit:
services.yml:
company.user_manager:
class: Company\CoreBundle\Model\UserManager
arguments: [%company.userManagerService%]
Then on config.yml:
parameters:
company.userManagerService:
em: 'doctrine.orm.entity_manager'
user_helper: 'company.helper.user'
fos_manipulator: 'fos_user.util.user_manipulator'

Symfony2 use Doctrine in ExceptionController

Hi I'm trying to use Doctrine inside the default ExeptionController but I get the following error:
Fatal error: Call to undefined method Symfony\Bundle\TwigBundle\Controller\ExceptionController::getDoctrine()
when I try to call:
$manager = $this->getDoctrine()->getManager();
What I'm trying to do is to have a custom 404 page where I can present some items from the database.
Could you please help me? Thank you!
You may also inject the Doctrine service as a dependency in your Controller (in that case you don't need to entend class Controller)
You will have to create your own ExceptionController extending the default one. You'll have to declare it as described here: http://symfony.com/doc/current/cookbook/controller/error_pages.html#custom-exception-controller. Your custom controller must have a constructor with at least an argument of type Registry (Doctrine). You have to declare that controller as a service in your service.yml (or xml depending on your config) Have a look at the symfony doc for further explanation on how to do that. For the moment I can't help you much more as I'm outside with my Android and it's rather difficult to make long answers

Resources