0
votes

maybe I just messed something up with namespaces, but I don't see it.

I tried to separate some classess in my Symfony App into a bundle.
The error I get in Symfony is:

Expected to find class "Shop\Admin\CategoryAdmin" in file "C:\wamp64\www\gall\bundles\Shop\src/Admin\CategoryAdmin.php" while importing services from resource "../bundles/Shop/src/*", but it was not found! Check the namespace prefix used with the resource in C:\wamp64\www\gall\config/services.yaml (which is loaded in resource "C:\wamp64\www\gall\config/services.yaml").


Here's my services.yaml:

Shop\:
    resource: '../bundles/Shop/src/*'
    exclude: '../bundles/Shop/src/{Entity,Migrations,Tests}'

Shop\Controller\:
    resource: '../bundles/Shop/src/Controller'
    tags: ['controller.service_arguments']

The file structure goes like this:

project (C:\wamp64\www\gall)
    \- bundles
        \- Shop
            \- src
                \- Admin
                    \- CategoryAdmin.php
    \- config
        \- services.yaml

And the CategoryAdmin.php file itself goes like this:

namespace Shop\Admin;

use Shop\Entity\Category;
use Sonata\AdminBundle\Admin\AbstractAdmin;
use Sonata\AdminBundle\Datagrid\ListMapper;
use Sonata\AdminBundle\Datagrid\DatagridMapper;
use Sonata\AdminBundle\Form\FormMapper;
use Symfony\Bridge\Doctrine\Form\Type\EntityType;
use Symfony\Component\Form\Extension\Core\Type\TextType;
use Symfony\Component\Form\Extension\Core\Type\CheckboxType;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Sonata\TranslationBundle\Filter\TranslationFieldFilter;


/**
 * Class CategoryAdmin
 * @package Shop\Admin
 */
class CategoryAdmin extends AbstractAdmin
{
    {...}
}

Soo, the routes look exactly like these stated in the error. I don't understand why the FileLoader can't find this class, any ideas?

1

1 Answers

1
votes

This is an autoloading issue.

composer.json part:

"autoload": {
    "psr-4": {
        "App\\": "src/",
    }
},

The App namespace is tied to the src folder.

Register the Shop namespace as bundles/Shop, and remove the src dir under bundles/Shop

EDIT: I usually do this for bundles I'm not ready to publish just yet, but will publish in the future. Example from one of my projects:

"autoload": {
    "psr-4": {
        "App\\": "src/",
        "Padam87\\AdminBundle\\": "bundles/Padam87/AdminBundle/"
    }
},

EDIT 2: Don't forget to run composer dump-autoload after the change.