Problem
I used this solution https://stackoverflow.com/a/15167450/2910183, but I have a following error when I'm trying open admin dashboard (http://localhost/app_dev.php/admin/dashboard
). This happens also after cleaning cache.
ClassNotFoundException: Attempted to load class "AcmeBlockService" from namespace "Acme\ProductBundle\Block" in ~/htdocs/symfony2training/app/cache/dev/appDevDebugProjectContainer.php line 2216. Do you need to "use" it from another namespace?
Does anybody know where is problem?
Code
Here is part of my 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 }
Part of my 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 }
My src/Acme/ProductBundle/Document/Products.php
file in :
<?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);
}
}
Solution
- I looked into another block (
vendor/sonata-project/user-bundle/Sonata/UserBundle/Block/AccountBlockService.php
) and I realized, that its file name is like class name. So I changedsrc/Acme/ProductBundle/Document/Products.php
tosrc/Acme/ProductBundle/Document/ProductsBlockService.php
- It works, but another error appeared:
FatalErrorException: Compile Error: Declaration of Acme\ProductBundle\Block\ProductsBlockService::execute() must be compatible with Sonata\BlockBundle\Block\BlockServiceInterface::execute(Sonata\BlockBundle\Block\BlockContextInterface $blockContext, Symfony\Component\HttpFoundation\Response $response = NULL) in ~/htdocs/symfony2training/src/Acme/ProductBundle/Block/ProductsBlockService.php line 0
- The solution is written in the error message: Acme\ProductBundle\Block\ProductsBlockService::execute() must be compatible with Sonata\BlockBundle\Block\BlockServiceInterface::execute(). So I looked into my helper (AccountBlockService.php from 1.), compare
execute()
methods and write my own solution.
So in the end my src/Acme/ProductBundle/Document/ProductsBlockService.php
file looks as below.
<?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);
}
}