如何在Symfony 4中使用libphonenumber.phone\u number\u util

如何在Symfony 4中使用libphonenumber.phone\u number\u util,symfony,symfony4,Symfony,Symfony4,要解析电话号码,我需要在控制器(Symfony 4)中使用libphonenumber.phone\u number\u util,如下所示: $parsed = $this->get('libphonenumber.phone_number_util')->parse($phoneNo); 由于我们私下里有libphonenumber.phone\u number\u util,我想通过在服务中添加此帮助程序来公开它,如下所示: services: libphonenumb

要解析电话号码,我需要在控制器(Symfony 4)中使用
libphonenumber.phone\u number\u util
,如下所示:

$parsed = $this->get('libphonenumber.phone_number_util')->parse($phoneNo);
由于我们私下里有
libphonenumber.phone\u number\u util
,我想通过在服务中添加此帮助程序来公开它,如下所示:

services:
   libphonenumber\PhoneNumberUtil:
      alias: libphonenumber.phone_number_util
      public: true
但这将返回异常和消息:

"message": "The \"libphonenumber.phone_number_util\" service or alias has been removed or inlined when the container was compiled. You should either make it public, or stop using the container directly and use dependency injection instead.",
            "class": "Symfony\\Component\\DependencyInjection\\Exception\\ServiceNotFoundException",

如果您在控制器方法中使用此方法(我假定您是基于
$this->get(…)
),则需要

1) 将控制器声明为服务,并使用
controller.service\u参数标记它

2) 确保您的util服务id与类名匹配(我想它已经匹配了)。你不需要公开它——这是一种古老的方法

3) 需要将util作为控制器操作方法的参数

例如

关于依赖关系管理中的这些变化,有一篇很好的Symfony博客文章:

services:
    libphonenumber\PhoneNumberUtil:
        alias: libphonenumber.phone_number_util

    AppBundle\Controller\MyController:
        tags: ['controller.service_arguments']
public function validatePhoneAction(Request $request, PhoneNumberUtil $phoneNumberUtil)
{
    ...
    $phoneNumberUtil->parse($request->request->get('phone_number');
    ...
}