我使用 symfony2 和 SonataAdminBundle、SonataMediaBundle 和 SonataClassificationBundle

现在我想要管理面板的自定义设置,但我有这个错误。

 [Symfony\Component\Config\Exception\FileLoaderLoadException]
  Catchable Fatal Error: Argument 5 passed to Sonata\MediaBundle\Admin\BaseMe
  diaAdmin::__construct() must implement interface Sonata\ClassificationBundl
  e\Model\CategoryManagerInterface, none given, called in /Users/whitebear/Codin
  gWorks/httproot/myapp/app/cache/de_/appDevDebugProjectContaine_.php on l
  ine 9494 and defined in . (which is being imported from "/Users/whitebear/Codi
  ngWorks/httproot/myapp/app/config/routing.yml").

我所做的是两件事。

制作依赖注入(inject)文件

应用程序/奏鸣曲/MediaBundle/DependencyInjection/ApplicationSonataMediaExtension.php
  <?php

namespace Application\Sonata\MediaBundle\DependencyInjection;

use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\Config\FileLocator;
use Symfony\Component\HttpKernel\DependencyInjection\Extension;
use Symfony\Component\DependencyInjection\Loader;

/**
 * This is the class that loads and manages your bundle configuration
 *
 * To learn more see {@link http://symfony.com/doc/current/cookbook/bundles/extension.html}
 */
class ApplicationSonataMediaExtension extends Extension
{
    /**
     * {@inheritDoc}
     */
    public function load(array $configs, ContainerBuilder $container)
    {
        $loader = new Loader\YamlFileLoader($container, new FileLocator(__DIR__.'/../Resources/config'));
        $loader->load('services.yml');
    }
}

然后为管理员制作 services.yml

应用程序/奏鸣曲/MediaBundle/Resources/config/services.yml
#Disable gallery & media menu from admin panel
services:
    sonata.media.admin.media:
        class: %sonata.media.admin.media.class%
        tags:
            - { name: sonata.admin, manager_type: orm, show_in_dashboard: false, label_catalogue: %sonata.media.admin.media.translation_domain% , label_translator_strategy: sonata.admin.label.strategy.underscore }
        arguments:
            - ~
            - %sonata.media.admin.media.entity%
            - %sonata.media.admin.media.controller%
            - "@sonata.media.pool"
            - %sonata.classification.manager.category% # add here.

        calls:
            - [setModelManager, ["@sonata.media.admin.media.manager"]]
            - [setTranslationDomain, [%sonata.media.admin.media.translation_domain%]]
            - [setTemplates, [{ inner_list_row: SonataMediaBundle:MediaAdmin:inner_row_media.html.twig , base_list_field: SonataAdminBundle:CRUD:base_list_flat_field.html.twig , list: SonataMediaBundle:MediaAdmin:list.html.twig , edit: SonataMediaBundle:MediaAdmin:edit.html.twig }]]

    sonata.media.admin.gallery:
        class: %sonata.media.admin.gallery.class%
        tags:
            - { name: sonata.admin, manager_type: orm, show_in_dashboard: false, label_catalogue: %sonata.media.admin.media.translation_domain% , label_translator_strategy: sonata.admin.label.strategy.underscore }
        arguments:
            - ~
            - %sonata.media.admin.gallery.entity%
            - %sonata.media.admin.gallery.controller%
            - "@sonata.media.pool"
        calls:
            - [setTranslationDomain, [%sonata.media.admin.media.translation_domain%]]
            - [setTemplates, [{ list: SonataMediaBundle:GalleryAdmin:list.html.twig }]]

在奏鸣曲\MediaBundle\Admin\BaseMediaAdmin
abstract class BaseMediaAdmin extends AbstractAdmin
{
    /**
     * @var Pool
     */
    protected $pool;

    /**
     * @var CategoryManagerInterface
     */
    protected $categoryManager;

    /**
     * @param string                   $code
     * @param string                   $class
     * @param string                   $baseControllerName
     * @param Pool                     $pool
     * @param CategoryManagerInterface $categoryManager
     */
    public function __construct($code, $class, $baseControllerName, Pool $pool, CategoryManagerInterface $categoryManager)
    {
        parent::__construct($code, $class, $baseControllerName);

        $this->pool = $pool;

        $this->categoryManager = $categoryManager;
    }

感谢@mdma
我发现我必须将第 5 个参数作为 CategoryManagerInterface 传递给 BaseMediaAdmin 构造函数。

然后我更新了 service.yml 但我有这样的错误。
 [Symfony\Component\DependencyInjection\Exception\ParameterNotFoundException]
  The service "sonata.media.admin.media" has a dependency on a non-existent p
  arameter "sonata.classification.manager.category". Did you mean one of thes
  e: "sonata.classification.manager.category.class", "sonata.classification.m
  anager.tag.class", "sonata.classification.manager.context.class", "sonata.c
  lassification.manager.tag.entity", "sonata.classification.manager.category.
  entity", "sonata.classification.manager.context.entity", "sonata.classifica
  tion.admin.category.class"?

它解决了。
我把这句话改成inservices.yml
- %sonata.classification.manager.category%"@sonata.classification.manager.category"

最佳答案

错误说:参数 5 不存在于 Sonata\MediaBundle\Admin\BaseMediaAdmin::__construct()

因此,请查看您的 Sonata.media.admin.media 服务配置中的参数。只有 4 个参数。您需要添加第 5 个。

在 bundle config ( https://github.com/sonata-project/SonataMediaBundle/blob/master/Resources/config/doctrine_orm_admin.xml ) 中,有 5 个参数:

<argument/>
<argument>%sonata.media.admin.media.entity%</argument>
<argument>%sonata.media.admin.media.controller%</argument>
<argument type="service" id="sonata.media.pool"/>
<argument type="service" id="sonata.media.manager.category" on-invalid="null"/>

所以,我认为你可以添加 @sonata.media.manager.category 作为第 5 个参数。

关于php - __construct() 必须实现接口(interface)错误,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/42384423/

10-12 06:45