【问题标题】:"The options "title", "url" do not exist. Defined options are: ""“选项“title”、“url”不存在。定义的选项是:“”
【发布时间】: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


【解决方案1】:

对于那些年后在谷歌上搜索的人:我最终放弃了教程并朝着相反的方向前进,基本上分叉了 Sonata 提供的现有逻辑。这是我记得做的一个模糊版本:

  • vendor/sonata-project/block-bundle/Block/Service/RssBlockService.php的文件克隆到src/AppBundle/Block/RssBlockService.php

  • 相应地更新了admin.ymlsonata_block.yml

  • 运行 tail -f var/logs/dev.log 并调整我的 use 语句以修补任何剩余的粗糙点。

  • 创建了一个新的 Twig 模板并调整了我的代码以使用该模板。

【讨论】:

    【解决方案2】:

    在这种情况下,错误消息令人困惑。

    这里需要设置的不是默认选项,而是默认设置,但是由于选项和设置都使用OptionsResolver类,所以反馈的措辞比较混乱。

    use Sonata\BlockBundle\Block\Service\AbstractBlockService;
    use Symfony\Component\OptionsResolver\OptionsResolver;
    
    class MyBlockService extends AbstractBlockService
    {
        public function configureSettings(OptionsResolver $resolver)
        {
            $resolver->setDefaults(array(
                'url'      => false,
                'title'    => 'Insert the rss title',
                'template' => '@SonataBlock/Block/block_core_rss.html.twig',
            ));
        }
    }
    
    • 我的示例使用 AbstractBlockService 扩展自 AbstractAdminBlockService。

    【讨论】:

      猜你喜欢
      • 2021-10-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-04-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多