使用Symfony2

时间:2016-07-20 19:04:18

标签: php symfony sonata-admin

所以,我全新安装Symfony 2.7.9的问题是SonataUserBundle没有更新他们的依赖关系而且我遇到了一个错误的捆绑包(我已经尝试升级到更高版本而没有成功)。

这是错误:

Attempted to call an undefined method named "setCurrentUri" of class "Knp\Menu\MenuItem".Did you mean to call "setCurrent"?

第91行的Sonata / Userbundle / Block / ProfileMenuBlockService.php中引用了

$menu->setCurrentUri($settings['current_uri']);

因此,我想在我的Application / Sonata / UserBundle中重载此文件,但我不明白它为什么不能工作。

这就是我的所作所为:

我复制了Application / Sonata / UserBundle / Block /

下负责的文件
<?php
namespace Application\Sonata\UserBundle\Block;

use Knp\Menu\ItemInterface;
use Knp\Menu\Provider\MenuProviderInterface;
use Sonata\AdminBundle\Validator\ErrorElement;
use Sonata\BlockBundle\Block\BlockContextInterface;
use Sonata\BlockBundle\Block\Service\MenuBlockService;
use Sonata\BlockBundle\Model\BlockInterface;
use Sonata\UserBundle\Menu\ProfileMenuBuilder;
use Symfony\Bundle\FrameworkBundle\Templating\EngineInterface;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\OptionsResolver\OptionsResolverInterface;

/**
 * Class ProfileMenuBlockService
 *
 * @package Sonata\UserBundle\Block
 *
 * @author Hugo Briand <briand@ekino.com>
 */
class ProfileMenuBlockService extends MenuBlockService
{
    /**
     * @var ProfileMenuBuilder
     */
    private $menuBuilder;

    /**
     * Constructor
     *
     * @param string                $name
     * @param EngineInterface       $templating
     * @param MenuProviderInterface $menuProvider
     * @param ProfileMenuBuilder    $menuBuilder
     */
    public function __construct($name, EngineInterface $templating, MenuProviderInterface $menuProvider, ProfileMenuBuilder $menuBuilder)
    {
        parent::__construct($name, $templating, $menuProvider, array());

        $this->menuBuilder = $menuBuilder;
    }

    /**
     * {@inheritdoc}
     */
    public function getName()
    {
        return 'User Profile Menu';
    }

    /**
     * {@inheritdoc}
     */
    public function setDefaultSettings(OptionsResolverInterface $resolver)
    {
        parent::setDefaultSettings($resolver);

        $resolver->setDefaults(array(
            'cache_policy' => 'private',
            'menu_template' => "SonataBlockBundle:Block:block_side_menu_template.html.twig",
        ));
    }

    /**
     * {@inheritdoc}
     */
    protected function getMenu(BlockContextInterface $blockContext)
    {
        $settings = $blockContext->getSettings();

        $menu = parent::getMenu($blockContext);

        if (null === $menu || "" === $menu) {
            $menu = $this->menuBuilder->createProfileMenu(
                array(
                    'childrenAttributes' => array('class' => $settings['menu_class']),
                    'attributes'         => array('class' => $settings['children_class']),
                )
            );
            $menu->setCurrent($settings['current_uri']); // The corrected line
        }

        return $menu;
    }
}

由于它没有工作,我还重载了配置文件Sonata / UserBundle / Resources / config / block.xml:

<?xml version="1.0" encoding="UTF-8" ?>
<container xmlns="http://symfony.com/schema/dic/services"
           xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
           xsi:schemaLocation="http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd">

    <services>
        <service id="sonata.user.block.menu" class="Application\Sonata\UserBundle\Block\ProfileMenuBlockService">
            <tag name="sonata.block" />
            <argument>sonata.user.block.menu</argument>
            <argument type="service" id="templating" />
            <argument type="service" id="knp_menu.menu_provider" />
            <argument type="service" id="sonata.user.profile.menu_builder" />
        </service>

        <service id="sonata.user.block.account" class="Sonata\UserBundle\Block\AccountBlockService">
            <tag name="sonata.block" />
            <argument>sonata.user.block.account</argument>
            <argument type="service" id="templating" />
            <argument type="service" id="security.context" />
        </service>
    </services>

</container>

你看到我遗失的任何东西吗?

更不用说我已经清除了缓存,甚至重启了我的电脑。

先谢谢。

1 个答案:

答案 0 :(得分:2)

在pavlovich的建议之后,我再次尝试this solution之前失败并再次失败。然后,我注意到正确答案的评论并尝试了它:它失败了,因为我已经使用了sonata-project / user-bundle 2.2.5。然后我使用以下命令:

composer require composer require knplabs/knp-menu ~1.1 knplabs/knp-menu-bundle ~1.1 sonata-project/user-bundle 2.2.4

哪个有效,但我收到另一个错误,缺少一个奏鸣曲服务的依赖。因此我回滚到sonata-project / user-bundle 2.2.5,它没有更新knplabs / * - bundle版本。

令我惊讶的是,它实际上解决了我的问题。