【发布时间】:2019-06-17 07:25:53
【问题描述】:
我正在我的系统中实现选民,但我不知道如何为选民创建/扩展/实施更通用的方式。
我有以下选民(缩小):
class EventVoter extends Voter {
private $roleBaseName = 'ROLE_EVENT';
private $classname = Event::class;
private $ownershipMethod = 'getCreatedBy';
protected function supports($attribute, $subject) {
// Only vote on {$this->classname} objects
if (!$subject instanceof $this->classname) {
return false;
}
return true;
}
protected function voteOnAttribute($attribute, $subject, TokenInterface $token) {
// Check if you own this specific entry:
return $subject->{$this->ownershipMethod} === $user;
}
}
这个可以正常工作。但是,我还想要一个用于 News、Page、Skill、Foo 和 Bar。
我可以复制这个选民并更改顶部变量。但由于这些是仅有的 3 个更改,我想要某种通用选民,我可以加载值,例如在构造上,产生更可靠的代码,真正的逻辑在一个地方。
但如果我用 Voter 扩展 Generic 类,它会自动加载它。我希望它忽略泛型类,并最终得到类似:
class EventVoter extends GenericVoter
{
private $roleBaseName = 'ROLE_EVENT';
private $classname = Event::class;
private $ownershipMethod = 'getCreatedBy';
// Possibly, if required at all:
protected function voteOnAttribute($attribute, $subject, TokenInterface $token){
return parent::voteOnAttribute($attribute, $subject,$token)
}
}
谁能指出我正确的方向?
【问题讨论】:
-
我想检查您是否可以查看新闻以及是否可以查看事件等。它们都有相同的逻辑要检查。你能写一个关于实体+接口的简单例子吗?听起来像是我可能想要的东西。并且抽象类可能也是,不确定你是否可以扩展那些,但值得一试