代码之家  ›  专栏  ›  技术社区  ›  Calcimicium

Symfony FOSUserBundle用户管理器扩展

  •  1
  • Calcimicium  · 技术社区  · 7 年前

    我正在开发一个由Symfony 3支持的RESTful应用程序,使用FOSRestBundle、FOSUserBundle和FOSOAuthBundle。

    类型错误:传递给FOS\UserBundle\Doctrine\UserManager的参数1:\uu construct()必须实现接口FOS\UserBundle\Util\PasswordUpdaterInterface,未给定,在/var/www/html/var/cache/dev/appDevDebugProjectContainer中调用。php在线2538

    以下是我的部分应用程序文件:

    # app/config/config.yml
    
    fos_user:
        # [...]
        service:
            user_manager: foo.user_manager
    
    # src/FooBundle/Resources/config/services.yml
    
    services:
        # [...]
        foo.user_manager:
            class: FooBundle\Service\UserManager
    
        FooBundle\Service\UserManager:
            arguments:
                $class: '%fos_user.model.user.class%'
    
    <?php
    
    // src/FooBundle/Service/UserManager.php
    
    namespace FooBundle\Service;
    
    use FOS\UserBundle\Doctrine\UserManager as BaseUserManager;
    
    class UserManager extends BaseUserManager implements UserManagerInterface
    {
        /**
        * {@inheritDoc}
        * @see \FooBundle\Service\UserManagerInterface::findUsersAtPage()
        */
        public function findUsersAtPage($usersPerPage, $page) {
            $offset = $usersPerPage * $page;
            return $this->getRepository()->findBy(array(), null, $usersPerPage, $offset);
        }
    }
    
    <?php
    
    // src/FooBundle/Service/UserManagerInterface.php
    
    namespace FooBundle\Service;
    
    use FOS\UserBundle\Model\UserManagerInterface as BaseUserManagerInterface;
    
    interface UserManagerInterface extends BaseUserManagerInterface
    {
        /**
         * Returns a collection with specified number of user instances at specified page.
         * @param int $usersPerPage The number of users per page.
         * @param int $page The page number.
         * @return \Traversable
         */
        public function findUsersAtPage($usersPerPage, $page);
    }
    

    <?php
    
    // var/cache/dev/appDevDebugProjectContainer.php
    
    class appDevDebugProjectContainer extends Container
    {
        // [...]
    
        protected function getFosUser_UserManagerService()
        {
        return $this->services['fos_user.user_manager'] = new \FOS\UserBundle\Doctrine\UserManager(${($_ = isset($this->services['fos_user.util.password_updater']) ? $this->services['fos_user.util.password_updater'] : $this->getFosUser_Util_PasswordUpdaterService()) && false ?: '_'}, ${($_ = isset($this->services['fos_user.util.canonical_fields_updater']) ? $this->services['fos_user.util.canonical_fields_updater'] : $this->getFosUser_Util_CanonicalFieldsUpdaterService()) && false ?: '_'}, ${($_ = isset($this->services['fos_user.object_manager']) ? $this->services['fos_user.object_manager'] : $this->getFosUser_ObjectManagerService()) && false ?: '_'}, 'FooBundle\\Entity\\User');
        }
    
        // [...]
    }
    

    当我使用我的时(没有参数):

    <?php
    
    // var/cache/dev/appDevDebugProjectContainer.php
    
    class appDevDebugProjectContainer extends Container
    {
        // [...]
    
        protected function getFosUser_UserManagerService()
        {
            return $this->services['fos_user.user_manager'] = new \FooBundle\Service\UserManager();
        }
    
        // [...]
    }
    

    拜托,有人能帮我吗?

    2 回复  |  直到 7 年前
        1
  •  2
  •   Calcimicium    7 年前

    好了,伙计们,这就是答案:

    服务参数必须在服务定义中设置,而不是在类服务定义中设置。这不是很清楚,所以比word更好,一些代码:

    # src/FooBundle/Resources/config/services.yml
    
    services:
        # [...]
        foo.user_manager:
            class: FooBundle\Service\UserManager
            arguments:
                $class: '%fos_user.model.user.class%'
    
        FooBundle\Service\UserManager: '@foo.user_manager'
    

        2
  •  1
  •   Anton G    7 年前

    Check this answer

    或者试试这个

    services:
        you_bundle.user_manager:
            class: YourBundle\Services\UserManager
            arguments: ["@fos_user.util.password_updater", "@fos_user.util.canonical_fields_updater", "@doctrine.orm.entity_manager", "%fos_user.model.user.class%"]