0

In file service.yaml i have:

parameters:
  security.allows.ip:
    - '127.0.0.1'
    - '127.0.0.2'

Or:

parameters:
  security.allows.ip: ['127.0.0.1', '127.0.0.2']

And configuration for DI:

services:
  _defaults:
    autowire: true
    autoconfigure: true
    public: false

And i want to configure service for class:

security.class:
    class: App\Class
    arguments:
      - '%security.allows.ip%'

And finally I have message:

Cannot autowire service "App\Class": argument "$securityConfiguration" of method "__construct()" must have a type-hint or be given a value explicitly.

And constructor definition is:

 public function __construct(array $securityConfiguration)

Could you help me with it? In symfony 2.8 it works, but for this configuration I have this error. Other sevices for type hint string is ok, but not for this class. If I add container interface to construct for this class and getting parameter by ->getParameter('security.allows.ip') it works. Why?

1
  • 1
    I suggest to edit the title of this post to make it more focused on the problem detail. Commented Feb 22, 2018 at 0:30

1 Answer 1

6

In order for autowire to work, the typehint need to match a service id. The problem here is that you have another class into which you are trying to inject your rather poorly named App\Class

class SomeOtherClass {
    public function __construct(App\Class $appClass)

When you created your AppClass service, you gave it an id of security.class. So autowire looks for a service id of App\Class, does not find it and then attempts to create one. And of course it cannot autowire an array.

One way to fix this is by using an alias:

security.class:
    class: App\Class
    arguments:
        - '%security.allows.ip%'

App\Class: '@security.class'

A second (recommended) approach is to do away with the security.class id completely

App\Class:
    arguments:
        - '%security.allows.ip%'

And if you really want to be the cool kid on the block, you can even drop the arguments keyword.

App\Class:
    $securityConfiguration: '%security.allows.ip%'
Sign up to request clarification or add additional context in comments.

2 Comments

This name is only example it is not really named class
But use an alias fix my problem. Thank you.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.