我使用了此解决方案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);
}
}
vendor/sonata-project/user-bundle/Sonata/UserBundle/Block/AccountBlockService.php
),我意识到,它的文件名就像类名一样。所以我将src/Acme/ProductBundle/Document/Products.php
更改为src/Acme/ProductBundle/Document/ProductsBlockService.php
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)
execute()
方法并编写我自己的解决方案。所以最后我的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);
}
}