2

我像这样使用来自 knpMenu 的 regexVoter

//This is on my createMenuAction
$this->matchingRoute($menu);

/**
 * @param $menu
 *
 * @return bool
 */
private function matchingRoute($menu)
{
    foreach ($menu->getChildren() as $child) {
        $childRegex = $child->getAttribute('regex');

        $voter = new RegexVoter($childRegex);
        $voterTest = $voter->matchItem($child);

        if ($voterTest == true && $this->request->getUri()) {
            $child->setCurrent(true);
        } else {
            $child->setCurrent(false);
        }
    }
}

这是有效的,但为我设置了一个当前类,所有具有正则表达式属性的孩子。这个选民将孩子的正则表达式与他们的路线进行比较,所以它一直都是真的。

我试图添加return true;

if ($voterTest == true && $this->request->getUri()) {
    $child->setCurrent(true);
    return true;
} else {
    $child->setCurrent(false);
}

但这将当前类设置在第一个使用 regexVoter 返回 true 的孩子身上。

我应该用这个选民检查这个正则表达式与当前路线吗?

我如何使用这个选民为好孩子设置当前班级?

谢谢,

4

0 回答 0