Dario
Dario

Reputation: 113

Error: Attempted to load class "ProductAdmin" from namespace "Admin\AdminBundle\Admin"

I'm trying to configure Sonata admin bundle, but I'm having this error:

Attempted to load class "ProductAdmin" from namespace "Admin\AdminBundle\Admin". Did you forget a "use" statement for another namespace?

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'));
}

Someone have an idea how to sloved this error ?

Thank you

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');
    }
}

autoload.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;

Upvotes: 1

Views: 1002

Answers (1)

Gerard van Helden
Gerard van Helden

Reputation: 1602

The error indicates that your class file could not be autoloaded properly. Check if the namespace matches your path, and when in doubt, add a file to the root of your project loading the class to check whether the autoload config works:

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

The fact that your source file states another path than your namespace structure indicates that your class is in the wrong place.

Upvotes: 1

Related Questions