如何在一个控制器中多次调用symfony2中的服务

时间:2016-06-14 10:46:45

标签: symfony

我的服务名为" sms"我需要在它的控制器中多次使用它:

$this->get('sms')->send($userCard->getUser()->getMobile(), "text to send for first time");

$this->get('sms')->send($userCard->getUser()->getMobile(), "my second text to send");

但是它总是发送第一条消息两次,我该怎么办?

class SmsListener {
    private $url="http://www.smsprovider.com/URLSend.aspx?Username=adminuser&Password=addminpassword&PortalCode=6496&Flash=0&Message=%s&Mobile=%s";
    public function __construct() {
    }

    //put your code here
    public function send($mobile,$message) {

        $this->url=sprintf($this->url,$message,$mobile);

        $url = str_replace(" ", '%20', $this->url);
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_URL, $url);
        $ret=curl_exec($ch);
        return $ret;
    }

}

2 个答案:

答案 0 :(得分:2)

首次调用SmsListener::send后,您的代码会自行中断。这是因为您在此处覆盖了使用过的网址模板:

$this->url=sprintf($this->url,$message,$mobile);

此后,不再有占位符(%s)出现。然后,任何进一步的调用都将使用第一次运行的URL。

您可以通过在函数内使用局部变量来解决此问题:

$url = sprintf($this->url, $message, $mobile);
$url = str_replace(" ", '%20', $url);

那就是说,你最好提取配置。这实际上会使服务可重用。 E.g:

<?php

namespace AppBundle\Sms;

use Symfony\Component\OptionsResolver\OptionsResolver;

class SmsListener
{
    /**
     * @var string
     */
    private $baseUrl;

    /**
     * @var array
     */
    private $defaultOptions;

    /**
     * SmsListener constructor.
     *
     * @param string $baseUrl
     * @param array  $defaultOptions
     */
    public function __construct($baseUrl, $defaultOptions = [])
    {
        $this->baseUrl = $baseUrl;
        $this->defaultOptions = $defaultOptions;
    }

    /**
     * @param string $mobile
     * @param string $message
     * @param array  $options
     *
     * @return mixed
     */
    public function send($mobile, $message, array $options = [])
    {
        $url = $this->generateUrl(array_merge($options, [
            'Message' => $message,
            'Mobile' => $mobile
        ]));

        $ch = curl_init();
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_URL, $url);

        return curl_exec($ch);
    }

    /**
     * @param array $options
     *
     * @return string
     */
    private function generateUrl(array $options)
    {
        return $this->baseUrl . '?' . http_build_query($this->resolveOptions($options));
    }

    /**
     * @param array $options
     *
     * @return array
     */
    private function resolveOptions(array $options)
    {
        return $this->configureOptions(new OptionsResolver())->resolve($options);
    }

    /**
     * @param OptionsResolver $resolver
     *
     * @return OptionsResolver
     */
    private function configureOptions(OptionsResolver $resolver)
    {
        return $resolver
            ->setDefaults($this->defaultOptions)
            ->setRequired(['Message', 'Mobile'])
        ;
    }
}

配置:

# app/config/services.yml

services:
    app.sms_listener:
        class: AppBundle\Sms\SmsListener
        arguments:
            - "http://www.smsprovider.com/URLSend.aspx"
            -
                Username: "adminuser"
                Password: "addminpassword"
                PortalCode: "6496"
                Flash: "0"

用法(例如在控制器内):

<?php

public function someAction()
{
    /** @var $srv \AppBundle\Sms\SmsListener */
    $srv = $this->get('app.sms_listener');

    // all defaults
    $srv->send('1234', 'foobar');

    // overwriting options
    $srv->send('4321', 'barfoo', [
        'Flash' => '1'
    ]);
}

答案 1 :(得分:-1)

在函数中生成你的url:

class SmsListener {

        public function __construct() {
        }

        //put your code here
        public function send($mobile,$message) {
            $baseUrl=$url="http://www.smsprovider.com/URLSend.aspx?Username=adminuser&Password=addminpassword&PortalCode=6496&Flash=0&Message=%s&Mobile=%s";
            $url=sprintf($baseUrl,$message,$mobile);

            $url = str_replace(" ", '%20', $url);
            $ch = curl_init();
            curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
            curl_setopt($ch, CURLOPT_URL, $url);
            $ret=curl_exec($ch);
            return $ret;
        }

    }