【问题标题】:Error: Attempted to load class "ProductAdmin" from namespace "Admin\AdminBundle\Admin"错误:尝试从命名空间“Admin\AdminBundle\Admin”加载类“ProductAdmin”
【发布时间】:2015-09-09 19:11:46
【问题描述】:

我正在尝试配置 Sonata 管理包,但出现此错误:

尝试从命名空间加载类“ProductAdmin” “管理员\管理员捆绑\管理员”。您是否忘记了“使用”声明 另一个命名空间?

protected function getSonata_Admin_ProductService() {
    $instance = new \Admin\AdminBundle\Admin\ProductAdmin('sonata.admin.product', 'Admin\AdminBundle\Entity\Product', 'SonataAdminBundle:CRUD'); 
    $instance->setTranslationDomain('AdminAdminBundle'); 
    $instance->setFormTheme(array(0 => 'SonataDoctrineORMAdminBundle:Form:form_admin_fields.html.twig'));
}

有人知道如何解决这个错误吗?

谢谢

Config.yml

imports:
    - { resource: parameters.yml }
    - { resource: security.yml }
    - { resource: services.yml }
    - { resource: @AdminAdminBundle/Resources/config/admin.yml }
# app/config/config.yml
sonata_block:
    default_contexts: [cms]
    blocks:
        # Enable the SonataAdminBundle block
        sonata.admin.block.admin_list:
            contexts:   [admin]
        # Your other blocks

Admin.yml

services:
    sonata.admin.Product:
        class: Admin\AdminBundle\Admin\ProductAdmin
        tags:
            - { name: sonata.admin, manager_type: orm, group: "Content", label: "Product" }
        arguments:
            - ~
            - Admin\AdminBundle\Entity\Product
            - ~
        calls:
            - [ setTranslationDomain, [AdminAdminBundle]]

ProductAdmin.php

<?php
// src/Acme/DemoBundle/Admin/PostAdmin.php

namespace Admin\AdminBundle\Admin;

use Sonata\AdminBundle\Admin\Admin;
use Sonata\AdminBundle\Datagrid\ListMapper;
use Sonata\AdminBundle\Datagrid\DatagridMapper;
use Sonata\AdminBundle\Form\FormMapper;

class ProductAdmin extends Admin
{
    // Fields to be shown on create/edit forms
    protected function configureFormFields(FormMapper $formMapper)
    {
        $formMapper
            ->add('title', 'text', array('label' => 'Post Title'))
            ->add('author', 'entity', array('class' => 'Admin\AdminBundle\Entity\Product'))
            ->add('body') //if no type is specified, SonataAdminBundle tries to guess it
        ;
    }

    // Fields to be shown on filter forms
    protected function configureDatagridFilters(DatagridMapper $datagridMapper)
    {
        $datagridMapper
            ->add('title')
            ->add('author')
        ;
    }

    // Fields to be shown on lists
    protected function configureListFields(ListMapper $listMapper)
    {
        $listMapper
            ->addIdentifier('title')
            ->add('slug')
            ->add('author')
        ;
    }
}

AdminAdminExtension.php

<?php

namespace Admin\AdminBundle\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 AdminAdminExtension extends Extension
{
    /**
     * {@inheritdoc}
     */
    public function load(array $configs, ContainerBuilder $container)
    {
        $configuration = new Configuration();
        $config = $this->processConfiguration($configuration, $configs);

        $loader = new Loader\YamlFileLoader($container, new FileLocator(__DIR__.'/../Resources/config'));
        $loader->load('services.yml');
        $loader->load('admin.yml');
    }
}

自动加载.php

<?php

use Doctrine\Common\Annotations\AnnotationRegistry;
use Composer\Autoload\ClassLoader;

/**
 * @var ClassLoader $loader
 */

$loader = require __DIR__.'/../vendor/autoload.php';

AnnotationRegistry::registerLoader(array($loader, 'loadClass'));

return $loader;

【问题讨论】:

    标签: php symfony admin sonata-admin


    【解决方案1】:

    该错误表明您的类文件无法正确自动加载。检查命名空间是否与您的路径匹配,如有疑问,请在项目的根目录中添加一个文件以加载类以检查自动加载配置是否有效:

    <?php
    require_once 'vendor/autoload.php';
    var_dump(class_exists('a\b\c'));
    

    您的源文件声明的路径不是您的命名空间结构,这表明您的类位于错误的位置。

    【讨论】:

    • 嗨,杰拉德,我是 symfony 的新手,所以我试图理解.. 我检查了命名空间,似乎一切正常。在我的自动加载配置中,我可以看到我没有加载项目奏鸣曲,也许是因为这个错误?我怎样才能在我的自动加载中添加这些行。 $loader->registerNamespaces(array( // ... 'Sonata' => DIR.'/../vendor/bundles', 'Exporter' => DIR .'/../vendor/exporter/lib', 'Knp\Bundle' => DIR.'/../vendor/bundles', 'Knp\Menu' => DIR .'/../vendor/KnpMenu/src', // ... ));
    • 这应该在你项目的 composer.json 的 "autoload" 部分下进行配置。也许那里有什么问题?请参阅getcomposer.org/doc/04-schema.md#autoload 了解更多信息。
    猜你喜欢
    • 2018-06-07
    • 1970-01-01
    • 2011-11-26
    • 2020-01-30
    • 1970-01-01
    • 2015-03-29
    • 2017-03-23
    • 2021-10-03
    • 1970-01-01
    相关资源
    最近更新 更多