【问题标题】:Unable to use the sylius services无法使用 sylius 服务
【发布时间】:2019-02-11 15:58:09
【问题描述】:

找不到服务“sylius.repository.product”:即使它存在于应用程序的容器中,“App\Controller\Shop\SubscribeBoxController”内的容器是一个较小的服务定位器,只知道“学说”,“ form.factory”、“http_kernel”、“parameter_bag”、“request_stack”、“router”、“security.authorization_checker”、“security.csrf.token_manager”、“security.token_storage”、“serializer”、“session”、“模板”和“树枝”服务。尝试使用依赖注入。

当我尝试在我的控制器上使用 sylius 服务时出现此错误:

public function index(Request $request)
{
    $subscribed = new Subscribed();
    $subscribeForm = $this->createForm(SubscribeType::class, $subscribed);
    $subscribeForm->handleRequest($request);

    if ($subscribeForm->isSubmitted() && $subscribeForm->isValid()) {
        $entityManager = $this->getDoctrine()->getManager();
        $entityManager->persist($subscribed);
        $entityManager->flush();

        return $this->redirectToRoute('subscribe_details');
    }

    /** @var ProductRepository $productRepository */
    $productRepository = $this->get('sylius.repository.product');
    $product = $productRepository->findAll();

    return $this->render('@SyliusShop/Subscribe/plan.html.twig', [
        'form' => $subscribeForm->createView(),
        'product' => $product,
    ]);
}

所以如果有人知道这个错误,请告诉我! :)

【问题讨论】:

  • 你试过用依赖注入代替吗?
  • 是的,但我有这个新错误:
  • 无法自动装配“App\Controller\Shop\SubscribeBoxController::details()”的参数 $productRepository:它引用了类“Sylius\Bundle\ProductBundle\Doctrine\ORM\ProductRepository”,但不存在此类服务.您可能应该将此类别名为以下现有服务之一:“sylius.repository.product”、“App\Repository\ProductRepository”。
  • 那么,如果您开始使用自动装配,您为自定义应用程序代码配置了什么?
  • 我找不到我应该配置什么...

标签: php symfony sylius


【解决方案1】:

Symfony 似乎正在从直接服务容器访问转向使用依赖注入在控制器内部获取服务。此外,Sylius 完全接受了这一点——他们自己的大多数控制器甚至不扩展旧的基础 Symfony 控制器。

您可以尝试两件事。首先,如果您想直接从服务容器访问服务(并且这些服务是公共的),您可以在构造函数中注入服务容器的实例。

private $fullServiceContainer;
public function __construct(
    \Symfony\Component\DependencyInjection\ContainerInterface $container
) {
    $this->fullServiceContainer = $container;
}

//...
$this->fullServiceContainer->get('sylius.repository.product')

其次,您可以自己注入服务如果该服务有一个看起来像 PHP 类的名称或别名。我看到sylius.repository.product服务存在于容器中

$ php bin/console debug:container sylius.repository.product

Information for Service "sylius.repository.product"
===================================================

 ---------------- --------------------------------------------------------- 
  Option           Value                                                    
 ---------------- --------------------------------------------------------- 
  Service ID       sylius.repository.product                                
  Class            Sylius\Bundle\CoreBundle\Doctrine\ORM\ProductRepository  
  Tags             -                                                        
  Public           yes                                                      
  Synthetic        no                                                       
  Lazy             no                                                       
  Shared           yes                                                      
  Abstract         no                                                       
  Autowired        no                                                       
  Autoconfigured   no                                                       
 ---------------- --------------------------------------------------------- 

但它的类 -- Sylius\Bundle\CoreBundle\Doctrine\ORM\ProductRepository 似乎没有链接到服务。

$ php bin/console debug:container ProductRepository                                                     
  No services found that match "ProductRepository".  

这意味着如果你想注入产品存储库,你需要在你自己的 Symfony 应用程序中创建一个别名,这开始超出单个 Stack Overflow 答案的范围。

如果您想深入了解 Sylius/Symfony 和服务容器,您可能会感兴趣的两篇文章(自链接)。

Symfony’s Service Container

Symfony: Autowiring Services https://alanstorm.com/symfonys-service-container/

【讨论】:

    【解决方案2】:

    您应该使用依赖注入,使用RepositoryInterface 和命名参数$productRepository

    Sylius 以这种方式声明自动装配。

    use Sylius\Component\Resource\Repository\RepositoryInterface;
    
    final class MyClass
    {
        public function __construct(
            private RepositoryInterface $productRepository
        ) {
        }
    }
    

    详情请见\Sylius\Bundle\ResourceBundle\DependencyInjection\Driver\Doctrine\DoctrineORMDriver

    【讨论】:

      猜你喜欢
      • 2017-02-21
      • 1970-01-01
      • 2023-01-30
      • 2014-07-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多