Symfony 2 Bundle 依赖问题

3
我创建了一个管理用户组权限的bundle。我想通过将其移动到vendors目录中使其独立于项目。
为了使这个bundle不可变,我将用户数据移动到了一个usermeta bundle中。
主bundle仅包含有关用户的用户名和电子邮件,而usermeta包含所有其他内容(姓名、出生日期等,根据项目需要)。
问题在于,主用户bundle旨在属于核心bundle组,每个使用相同的项目都会使用它。 现在用户-用户元关系创建了一种依赖关系,因此每个项目都需要它。
我的问题是: - 如何标准化其格式,以强制在每个项目中正确创建它。 - 如何使此依赖关系变为可选(首选)
1个回答

5

我建议您在bundle中处理用户界面(UserInterface),而不是用户实体(User)。

如果Symfony的UserInterface没有实现您需要的所有内容(如:只有用户名但没有电子邮件),请在您的bundle中创建自己的UserInterface:

namespace YourDomain\YourBundle\Interface;

use Symfony\Component\Security\Core\User\UserInterface as BaseInterface;

/**
 * UserInterface is the interface that user classes must implement.
 */
interface UserInterface extends BaseInterface
{
    /**
     * Returns the email address of the user.
     *
     * @return string The user email address
     */
    function getEmail();
}

然后,在使用您的捆绑包的项目中,您的用户实体必须实现您的特定接口,而不是Symfony UserInterface。


为了完整起见,您可以参考官方文档:http://symfony.com/doc/master/cookbook/doctrine/resolve_target_entity.html - Francesco Casula

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接