模型/非控制器类中的方法调用和设置器注入

时间:2019-05-03 19:28:47

标签: php symfony

请声

我已经阅读了Symfony 4文档中的“服务容器”条目以及几乎所有链接的条目。我已经两次分别解决这个问题,每次花费4-6个小时。我显然只是不明白...请帮助!

想要

我想使用依赖注入在模型(用户)中使用服务(SessionInterface),但是我不想使用模型构造函数,因为那样的话,我每次水合并实例化模型时都需要注入依赖。我认为我应该能够使用方法调用和setter注入来满足此条件。

代码

services.yaml

services:
    _defaults:
        autowire: true
        autoconfigure: true
        public: false

    App\:
        resource: '../src/*'
        exclude: '../src/{DependencyInjection,Entity,Migrations,Tests,Kernel.php}'

    App\Controller\:
        resource: '../src/Controller'
        tags: ['controller.service_arguments']

型号

namespace App\Model\System\User;

use App\Model\System\Client\Client;
use Symfony\Component\HttpFoundation\Session\SessionInterface;
use Symfony\Component\Security\Core\User\{EquatableInterface, UserInterface};

class User implements EquatableInterface, UserInterface
{
    private $session;

    public function __construct(array $properties)
    {
        // stuff
    }

    public function clientAssumed(): Client
    {
        return $this->session->get('foo', 'bar');
    }

    /**
     * @required
     *
     * @param SessionInterface $session
     */
    public function setSession(SessionInterface $session)
    {
        $this->session = $session;
    }
}

控制器

namespace App\Controller\Core;

use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Component\Security\Core\Security;

class CoreBase extends AbstractController
{
    /**
     * Displays authenticated User landing page.
     *
     * @Route("/home", name="core_home")
     *
     * @param Security $security
     *
     * @return Response
     */
    public function landing(Security $security): Response
    {
        $data = [
            'client' => $security->getUser()->clientAssumed(),
        ];

        return $this->render('core/landing.html.twig', $data);
    }
}

服务信息

php bin/console debug:container 'App\Model\System\User\User'

Information for Service "App\Model\System\User\User"
====================================================

 ---------------- ---------------------------- 
  Option           Value                       
 ---------------- ---------------------------- 
  Service ID       App\Model\System\User\User  
  Class            App\Model\System\User\User  
  Tags             -                           
  Calls            setSession                  
  Public           no                          
  Synthetic        no                          
  Lazy             no                          
  Shared           yes                         
  Abstract         no                          
  Autowired        yes                         
  Autoconfigured   yes                         
 ---------------- ---------------------------- 

结果

Too few arguments to function App\Model\System\User\User::setSession(), 0 passed in /srv/http/wwwroot/gw4/src/Controller/Core/CoreBase.php on line 25 and exactly 1 expected

0 个答案:

没有答案