Symfony 2 Service Parameters

允我心安 提交于 2020-01-05 07:25:33

问题


One of the parameters I want to pass to a service when creating it is a result of a string calculation based on some user inputs. How can this be achieved automagically such that I do not have to pass the values into the object every time I want to access it from a different controller?

Just to illustrate I am adding the following example:

I have encapsulated the Instagram API in a Service by following the Documentation (brilliant), so writing $instagram = $this->get('instagram'); in my code almost works, as for the communication with the API I need to also set a callback URL, which I currently always have to do after the service is retrieved, for eg:

    $redirect_url   = $this->getRequest()->getSchemeAndHttpHost().$this->generateUrl('_instagram_authenticate_response');
    $instagram      = $this->get('instagram');
    $instagram->setApiCallback($redirect_url);

It is okay to do this once, but not in every freaking method I use when I talk to instagram. Please help :)

Update: My Service definition is as follows:

parameters:
    client_id: xxxxx
    client_secret: yyyyy

services:
    instagram:
        class: Clops\InstagramBundle\InstagramAPI
        arguments: ["%client_id%", "%client_secret%"]

The question is --> how do I pass the third parameter as a value generated in a controller? Or do I always have to do this via setValue(X) after I get the service?


回答1:


It looks like a Dependency Injection issue.
As mentionned, you just have to pass @router and @request as a parameter of your service.

However, it seems a one-method only dependency so here's my way of thinking

You should create a handleApiCallback method which will be called immediatly after instanciation, in which you'll pass your @router and @request
This allows your class not to be strongly tied to Symfony

As a side note, you cannot pass directly @request as a parameter, you need to declare your service as request-scoped

services.yml

services:
    instagram:
        class: Clops\InstagramBundle\InstagramAPI
        scope: request
        arguments:
            - %client_id%
            - %client_secret%
        calls:
            - [ handleApiCallback, [ @request, @router ] ]

Clops\InstagramBundle\InstagramAPI

use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Routing\RouterInterface;

class InstagramApi
{
    public function handleApiCallback(Request $request, RouterInterface $router)
    {
        $host  = $request->getSchemeAndHttpHost();
        $route = $router->generateUrl('_instagram_authenticate_response');
        $uri   = $host.$route;

        $this->setApiCallback($uri);
    }
}



回答2:


  1. You can generate your parameter internally in service. In your situation you can pass request and router to service and make a work.

  2. Make a base controller that will have method getInstagram().

    public function getInstagram() 
    {
        $redirect_url = $this->getRequest()->getSchemeAndHttpHost().$this->generateUrl('_instagram_authenticate_response');
        $instagram = $this->get('instagram');
        $instagram->setApiCallback($redirect_url);
        return $instagram;
    }
    


来源:https://stackoverflow.com/questions/18293816/symfony-2-service-parameters

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!