Symfony 2 服务参数 [英] Symfony 2 Service Parameters

查看:30
本文介绍了Symfony 2 服务参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在创建服务时传递给服务的参数之一是基于某些用户输入的字符串计算结果.如何自动实现这一点,以便每次我想从不同的控制器访问对象时都不必将值传递给对象?

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:

我已经按照文档(很棒)将 Instagram API 封装在服务中,因此在我的代码中编写 $instagram = $this->get('instagram'); 几乎有效,至于与API的通信我还需要设置一个回调URL,我目前在检索服务后总是必须这样做,例如:

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);

这样做一次是可以的,但不是在我与 instagram 交谈时使用的每一种该死的方法中.请帮忙:)

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%"]

问题是 --> 如何将第三个参数作为在控制器中生成的值传递?还是我在获得服务后总是必须通过 setValue(X) 执行此操作?

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?

推荐答案

看起来像是依赖注入问题.
如前所述,您只需将 @router@request 作为服务的参数传递.

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

您应该创建一个 handleApiCallback 方法,该方法将在实例化后立即调用,您将在其中传递您的 @router@request
这使您的类不会与 Symfony 紧密相关

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

作为旁注,您不能直接将 @request 作为参数传递,您需要将您的服务声明为 请求范围

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);
    }
}

这篇关于Symfony 2 服务参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆