我创建了一个块,并在块内添加了一些内容。我想在页面 myblock.html.twig 内呈现我的块的内容
这是页面:myblock.html.twig
{% extends sonata_block.templates.block_base %}
{% block block %}
{{ sonata_block_render({'type':'sonata.block.service.myblock'}) }}
{% endblock %}
刷新页面时出现以下错误:
此网页无法使用
ERR_CONNECTION_RESET
我的服务器仍在运行,我可以访问该网站的其他页面。
MyBlockService.php
<?php
namespace FLY\BookingsBundle\Block;
use Symfony\Component\HttpFoundation\Response;
use Sonata\AdminBundle\Form\FormMapper;
use Sonata\CoreBundle\Validator\ErrorElement;
use Sonata\BlockBundle\Model\BlockInterface;
use Sonata\BlockBundle\Block\BlockContextInterface;
use Sonata\BlockBundle\Block\BaseBlockService;
use Doctrine\ORM\EntityManager;
use Symfony\Component\OptionsResolver\OptionsResolver;
use Symfony\Component\OptionsResolver\OptionsResolverInterface;
class MyBlockService extends BaseBlockService
{
private $em;
/**
* {@inheritdoc}
*/
public function execute(BlockContextInterface $block, Response $response = null)
{
// merge settings
$settings = array_merge($this->getDefaultSettings(), $block->getSettings());
return $this->renderResponse('FLYBookingsBundle:Default:myblock.html.twig', array(
'block' => $block,
'settings' => $settings
), $response);
}
/**
* {@inheritdoc}
*/
public function validateBlock(ErrorElement $errorElement, BlockInterface $block)
{
// TODO: Implement validateBlock() method.
}
/**
* {@inheritdoc}
*/
public function buildEditForm(FormMapper $formMapper, BlockInterface $block)
{
$formMapper->add('settings', 'sonata_type_immutable_array', array(
'keys' => array(
array('content', 'textarea', array()),
)
));
}
/**
* {@inheritdoc}
*/
public function getName()
{
return 'Text (core)';
}
/**
* {@inheritdoc}
*/
public function getDefaultSettings()
{
return array();
}
/**
* {@inheritdoc}
*/
public function configureSettings(OptionsResolver $resolver)
{
$resolver->setDefaults(array(
'content' => 'Insert your custom content here',
));
}
public function __construct($name, $templating, EntityManager $entityManager)
{
parent::__construct($name, $templating);
$this->em = $entityManager;
}}
服务.yml
sonata.block.service.myblock:
class: FLY\BookingsBundle\Block\MyBlockService
arguments: [ "sonata.block.service.myblock", @templating, @doctrine.orm.entity_manager ]
tags:
- { name: sonata.block }