Makros
Makros

Reputation: 352

Symfony Custom helper with parameters

I'm developing a Symfony App and I need to code a helper as a service. First of all I've defined 3 constants as parameters to take them in all web app

This is part of parameters content:

parameters:
    api.public.key: '123456789'
    api.private.key: 'ABCDEFGHI'
    api.timestamp: '1'

Now I'm going to develope my custom helper Utils/ApiHelper.php but I need that my helper can recieve this 3 parameters.

I've my Helper defined my helper as service like this:

services:
    api.helper:
        class: AppBundle\Utils\ApiHelper
        public: true

Upvotes: 0

Views: 729

Answers (1)

T. AKROUT
T. AKROUT

Reputation: 1717

You can add arguments to your custom service:

services:
    api.helper:
        class: AppBundle\Utils\ApiHelper
        public: true
        arguments: ['%api.public.key%', '%api.private.key%', '%api.timestamp%']

And then pass it as constructor arguments:

class ApiHelper
{
    // ...
    public function __construct(string $publicKey, string $privateKey, string $timeStamp)
    {
        // ...
    }
}

Upvotes: 1

Related Questions