在Sonata Admin Bundle中创建自定义块会导致错误

时间:2014-01-20 15:11:28

标签: php symfony sonata-admin symfony-sonata symfony-2.4

问题

我使用了此解决方案https://stackoverflow.com/a/15167450/2910183,但在尝试打开管理信息中心(http://localhost/app_dev.php/admin/dashboard)时出现以下错误。清理缓存后也会发生这种情况。

  

ClassNotFoundException:尝试从〜/ htdocs / symfony2training / app / cache / dev / appDevDebugProjectContainer.php第2216行中的命名空间“Acme \ ProductBundle \ Block”加载类“AcmeBlockService”。您是否需要从另一个“使用”它命名空间?

有人知道问题出在哪里吗?

代码

以下是我app/config/config.yml

的一部分
sonata_block:
    default_contexts: [cms]
    blocks:
        sonata.admin.block.admin_list:
            contexts:   [admin]
        sonata.user.block.menu:     # used to display the menu in profile pages
        sonata.user.block.account:  # used to display menu option (login option)
        acme.block.products:

sonata_admin:
    dashboard:
        blocks:
            # display a dashboard block
            - { position: left, type: acme.block.products }
            - { position: left, type: sonata.admin.block.admin_list }

我的src/Acme/ProductBundle/Resources/config/services.yml

的一部分
services:
    acme.block.products:
        id: acme.block.products
        class: Acme\ProductBundle\Block\ProductsBlockService
        arguments:
            - { name: service, id: templating }
        tags:
            - { name: sonata.block }

我的src/Acme/ProductBundle/Document/Products.php文件位于:

<?php
namespace Acme\ProductBundle\Block;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\OptionsResolver\OptionsResolverInterface;
use Sonata\BlockBundle\Model\BlockInterface;
use Sonata\BlockBundle\Block\BlockContextInterface;
use Sonata\AdminBundle\Form\FormMapper;
use Sonata\AdminBundle\Validator\ErrorElement;
class ProductsBlockService extends BaseBlockService {
    public function getName() {
        return 'Products';
    }
    public function getDefaultSettings() {
        return array();
    }
    public function validateBlock(ErrorElement $errorElement, BlockInterface $block) {
    }
    public function buildEditForm(FormMapper $formMapper, BlockInterface $block) {
    }
    public function execute(BlockInterface $block, Response $response = null) {
        $settings = array_merge($this->getDefaultSettings(), $block->getSettings());
        return $this->renderResponse('AcmeProductBundle:Block:admin_products.html.twig', array(
                'block' => $block,
                'settings' => $settings
        ), $response);
    }
}

解决方案

  1. 我查看了另一个块(vendor/sonata-project/user-bundle/Sonata/UserBundle/Block/AccountBlockService.php),我意识到,它的文件名就像类名一样。所以我将src/Acme/ProductBundle/Document/Products.php更改为src/Acme/ProductBundle/Document/ProductsBlockService.php
  2. 它有效,但出现了另一个错误:
      

    FatalErrorException:编译错误:Acme \ ProductBundle \ Block \ ProductsBlockService :: execute()声明必须与Sonata \ BlockBundle \ Block \ BlockServiceInterface :: execute兼容(Sonata \ BlockBundle \ Block \ BlockContextInterface $ blockContext,Symfony \ Component \ ~htdocs / symfony2training / src / Acme / ProductBundle / Block / ProductsBlockService.php第0行中的\ HttpFoundation \ Response $ response = NULL)

  3. 解决方案写在错误消息中: Acme \ ProductBundle \ Block \ ProductsBlockService :: execute()必须与Sonata \ BlockBundle \ Block \ BlockServiceInterface :: execute()兼容。所以我查看了我的助手(来自1的 AccountBlockService.php ),比较execute()方法并编写我自己的解决方案。
  4. 所以最后我的src/Acme/ProductBundle/Document/ProductsBlockService.php文件如下所示。

    <?php
    namespace Acme\ProductBundle\Block;
    use Sonata\AdminBundle\Form\FormMapper;
    use Sonata\AdminBundle\Validator\ErrorElement;
    use Sonata\BlockBundle\Block\BaseBlockService;
    use Sonata\BlockBundle\Block\BlockContextInterface;
    use Sonata\BlockBundle\Model\BlockInterface;
    use Sonata\UserBundle\Menu\ProfileMenuBuilder;
    use Sonata\UserBundle\Model\UserInterface;
    use Symfony\Bundle\FrameworkBundle\Templating\EngineInterface;
    use Symfony\Component\HttpFoundation\Response;
    use Symfony\Component\OptionsResolver\OptionsResolverInterface;
    use Symfony\Component\Security\Core\SecurityContextInterface;
    class ProductsBlockService extends BaseBlockService {
        public function getName() {
            return 'Products';
        }
        public function validateBlock(ErrorElement $errorElement, BlockInterface $block) {
        }
        public function buildEditForm(FormMapper $formMapper, BlockInterface $block) {
        }
        public function setDefaultSettings(OptionsResolverInterface $resolver) {
            $resolver->setDefaults(array(
                'template' => 'AcmeProductBundle:Block:admin_products.html.twig',
                'ttl' => 0
            ));
        }
        public function execute(BlockContextInterface $blockContext, Response $response = null) {
            return $this->renderPrivateResponse($blockContext->getTemplate(), array(
                    'block' => $blockContext->getBlock(),
                    'context' => $blockContext,
                ), $response);
        }
    }
    

0 个答案:

没有答案