1

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?

Arkadiusz Galler
  • 305
  • 3
  • 18

1 Answers1

2

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.

Padam87
  • 1,013
  • 1
  • 6
  • 7
  • Is there any solution that doesn't require removing src folder? I'd like to maintain default Sf structure inside my bundle (classess in src, templates, translations, etc next to src directory). Also is there any option not to include "bundles" in the namespace and configure autoloader for working "Shop" namespace? I'll mess with it, thanks for some directions. – Arkadiusz Galler Jul 25 '18 at 19:20
  • 1
    Bundle structure is different from application structure. Most bundles don't use an src folder. BTW, if you are doing this to add some structure to your app, don't do it at all. Use folders under the regular App namespace. – Padam87 Jul 25 '18 at 19:23
  • Generally, bundle structure was encouraged in Sf3, in Sf4 it's mostly discouraged. I want to separate "Shop bundle" so I can easily remove files and few config lines from the project any time without going through all directories and removing "shop" directory with bundle related files. I'll mess with it some more, thank you for hints and more hints on my issue are nice to see :) – Arkadiusz Galler Jul 25 '18 at 19:26
  • For clarification - I'm trying to build some multipurpose app and I want my structure to be easly modified considering adding and removing core features of the app. – Arkadiusz Galler Jul 25 '18 at 19:29