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

查看:121
本文介绍了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 = $这个 - >获取('Instagram的'); 在我的code 几乎的作品,作为与我需要还设置了回调URL,这是我现在总是有服务检索后,如做API通讯:

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?

推荐答案

它看起来像一个依赖注入的问题。结果
正如mentionned,你只需要通过 @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 方法,它将instanciation后立刻被调用,在其中你会通过你的 @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天全站免登陆