【发布时间】:2018-08-03 12:46:06
【问题描述】:
我正在学习本教程:
https://sonata-project.org/bundles/block/master/doc/reference/your_first_block.html
我可以保存一个新块,但是当我尝试使用 remove 按钮来删除块时,我的日志显示此错误:
request.CRITICAL:未捕获的 PHP 异常 Symfony\Component\OptionsResolver\Exception\UndefinedOptionsException: “选项“title”、“url”不存在。定义的选项是:“”。”在 /usr/src/app/vendor/symfony/symfony/src/Symfony/Component/OptionsResolver/OptionsResolver.php 第 685 行
我需要在哪里定义我的字段才能解决此问题?如果遵循相关教程,该定义会是什么样子?
====
编辑1:这是我的块服务的内容:
<?php
namespace AppBundle\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\CoreBundle\Validator\ErrorElement;
use Sonata\BlockBundle\Block\Service\AbstractBlockService;
use Sonata\BlockBundle\Block\Service\AbstractAdminBlockService;
class PatrickBlockService extends AbstractAdminBlockService
{
public function configureOptions(OptionsResolver $resolver)
{
$resolver->setDefaults(array(
'url' => false,
'title' => 'Insert the rss title',
'template' => '@SonataBlock/Block/block_core_rss.html.twig',
));
}
public function buildEditForm(FormMapper $formMapper, BlockInterface $block)
{
$formMapper
->add('settings', 'sonata_type_immutable_array', array(
'keys' => array(
array('url', 'url', array('required' => false)),
array('title', 'text', array('required' => false)),
)
))
;
}
public function validateBlock(ErrorElement $errorElement, BlockInterface $block)
{
$errorElement
->with('settings.url')
->assertNotNull(array())
->assertNotBlank()
->end()
->with('settings.title')
->assertNotNull(array())
->assertNotBlank()
->assertMaxLength(array('limit' => 50))
->end()
;
}
public function execute(BlockContextInterface $blockContext, Response $response = null)
{
// merge settings
$settings = $blockContext->getSettings();
$feeds = false;
if ($settings['url']) {
$options = array(
'http' => array(
'user_agent' => 'Sonata/RSS Reader',
'timeout' => 2,
)
);
// retrieve contents with a specific stream context to avoid php errors
$content = @file_get_contents($settings['url'], false, stream_context_create($options));
if ($content) {
// generate a simple xml element
try {
$feeds = new \SimpleXMLElement($content);
$feeds = $feeds->channel->item;
} catch (\Exception $e) {
// silently fail error
}
}
}
return $this->renderResponse($blockContext->getTemplate(), array(
'feeds' => $feeds,
'block' => $blockContext->getBlock(),
'settings' => $settings
), $response);
}
}
...这里是来自 admin.yml 的相关内容:
app.block.service.patrick:
class: AppBundle\Block\PatrickBlockService
arguments:
- "Patrick Block"
- "@templating"
到目前为止,我已经尝试从 configureOptions() 方法中删除涉及的两个字段,但这似乎没有什么区别。
====
编辑2:
当我说“我要保存一个新块”时,我的意思是信息已保存在数据库中,可供以后检索。但是,我在该步骤中看到了相同的错误。
【问题讨论】:
-
你应该多隔离问题,很多人没时间陪你做教程
-
添加你正在使用的代码,这样我们就可以看到你写了什么
-
顺便说一句,你定义了 configureOptions 方法吗?
-
@vivoconunxino:我确实定义了configureOptions。好问题。
标签: php symfony sonata-admin sonata