0
votes

I have a freshly installed (through composer) Symfony app. I've moved things around a bit, and it seems this has caused Symfony's annotation driver for controllers to break down. Most notably, I'm using a namespace.

My app/config/routing.yml is unmodified. I.e.:

app:
    resource: '@Kafoso/TestApp/AppBundle/Controller/'
    type: annotation

And app/config/routing_dev.yml points to app/config/routing.yml.

Now, I've moved the AppBundle class to a new namespace (and thus folder location) Kafoso\TestApp\AppBundle.

I have a default controller Kafoso\TestApp\AppBundle\Controller\DefaultController for GET /:

namespace Kafoso\TestApp\AppBundle\Controller;

use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\Request;

class DefaultController extends Controller
{
    /**
     * @Route("/", name="homepage")
     */
    public function indexAction(Request $request)
    {
        // replace this example code with whatever you need
        return $this->render('default/index.html.twig', [
            'base_dir' => realpath($this->getParameter('kernel.project_dir')).DIRECTORY_SEPARATOR,
        ]);
    }
}

The bundle is indeed added to app/AppKernel.php (new Kafoso\TestApp\AppBundle()).

The error I get:

request.CRITICAL: Uncaught PHP Exception Symfony\Component\Config\Exception\FileLoaderLoadException: "Cannot load resource "@Kafoso/TestApp/AppBundle/Controller/". Make sure the "Kafoso" bundle is correctly registered and loaded in the application kernel class. If the bundle is registered, make sure the bundle path "@Kafoso/TestApp/AppBundle/Controller/" is not empty." at (...)/vhosts/testapp/framework/server/vendor/symfony/symfony/src/Symfony/Component/Config/Loader/Loader.php line 73

Notice how Symfony thinks my bundle's name is "Kafoso".

The strange thing is, if I put an error_log or var_dump in the DefaultController (outside the class declaration), I see the output. But for some reason Symfony doesn't parse the controller's annotations.

Is it not possible to use namespaces here? Must everything be placed under AppBundle (or a differently chosen name)?

If it's the latter it seems like a rather restrictive and unnecessary enforcement.


EDIT

app/config/services.yml:

# Learn more about services, parameters and containers at
# https://symfony.com/doc/current/service_container.html
parameters:
    #parameter_name: value

services:
    # default configuration for services in *this* file
    _defaults:
        # automatically injects dependencies in your services
        autowire: true
        # automatically registers your services as commands, event subscribers, etc.
        autoconfigure: true
        # this means you cannot fetch services directly from the container via $container->get()
        # if you need to do this, you can override this setting on individual services
        public: false

    # makes classes in src/Kafoso/TestApp/AppBundle available to be used as services
    # this creates a service per class whose id is the fully-qualified class name
    Kafoso\TestApp\AppBundle\:
        resource: '../../src/Kafoso/TestApp/AppBundle/*'
        # you can exclude directories or files
        # but if a service is unused, it's removed anyway
        exclude: '../../src/Kafoso/TestApp/AppBundle/{Entity}'

    # controllers are imported separately to make sure they're public
    # and have a tag that allows actions to type-hint services
    Kafoso\TestApp\AppBundle\Controller\:
        resource: '../../src/Kafoso/TestApp/AppBundle/Controller'
        public: true
        tags: ['controller.service_arguments']

    # add more services, or override services that need manual wiring
    # Kafoso\TestApp\AppBundle\Service\ExampleService:
    #     arguments:
    #         $someArgument: 'some_value'

EDIT

composer.json:

{
    "name": "kafoso/testapp",
    "license": "proprietary",
    "type": "project",
    "authors": [
        {
            "name": "Kafoso",
            "email": ""
        }
    ],
    "autoload": {
        "psr-4": {
            "Kafoso\\TestApp\\": "src/Kafoso/TestApp"
        },
        "classmap": [
            "app/AppKernel.php",
            "app/AppCache.php"
        ]
    },
    "autoload-dev": {
        "psr-4": {
            "Kafoso\\TestApp\\AppBundle\\Tests\\Integration\\": "tests/integration",
            "Kafoso\\TestApp\\AppBundle\\Tests\\Unit\\": "tests/unit"
        },
        "files": [
            "vendor/symfony/symfony/src/Symfony/Component/VarDumper/Resources/functions/dump.php"
        ]
    },
    "require": {
        "php": "7.1.*",
        "doctrine/doctrine-bundle": "^1.6",
        "doctrine/orm": "^2.5",
        "incenteev/composer-parameter-handler": "^2.0",
        "sensio/distribution-bundle": "^5.0.19",
        "sensio/framework-extra-bundle": "^3.0.2",
        "symfony/monolog-bundle": "^3.1.0",
        "symfony/polyfill-apcu": "^1.0",
        "symfony/swiftmailer-bundle": "^2.3.10",
        "symfony/symfony": "3.3.*",
        "twig/twig": "^1.0||^2.0",
        "webmozart/glob": "^4.1"
    },
    "require-dev": {
        "phpunit/phpunit": "^6.2",
        "phpunit/dbunit": "^3.0",
        "sensio/generator-bundle": "^3.0",
        "symfony/phpunit-bridge": "^3.0"
    },
    "scripts": {
        "symfony-scripts": [
            "Incenteev\\ParameterHandler\\ScriptHandler::buildParameters",
            "Sensio\\Bundle\\DistributionBundle\\Composer\\ScriptHandler::buildBootstrap",
            "Sensio\\Bundle\\DistributionBundle\\Composer\\ScriptHandler::clearCache",
            "Sensio\\Bundle\\DistributionBundle\\Composer\\ScriptHandler::installAssets",
            "Sensio\\Bundle\\DistributionBundle\\Composer\\ScriptHandler::installRequirementsFile",
            "Sensio\\Bundle\\DistributionBundle\\Composer\\ScriptHandler::prepareDeploymentTarget"
        ],
        "post-install-cmd": [
            "@symfony-scripts"
        ],
        "post-update-cmd": [
            "@symfony-scripts"
        ]
    },
    "config": {
        "sort-packages": true
    },
    "extra": {
        "symfony-app-dir": "app",
        "symfony-bin-dir": "bin",
        "symfony-var-dir": "var",
        "symfony-web-dir": "../../www",
        "symfony-tests-dir": "tests",
        "symfony-assets-install": "relative",
        "incenteev-parameters": {
            "file": "app/config/parameters.yml"
        },
        "branch-alias": null
    }
}

The only changes from the vanilla file are:

  • Test specification of test namespaces, which have no influence on this problem.
  • The addition of the package webmozart/glob.
  • The change of symfony-web-dir, which is indeed working (www/app.php is run).

The PSR-4 autoload is Kafoso\TestApp, and not Kafoso\TestApp\AppBundle, but this shouldn't be necessary.

5
Show us the namespace section of your composer.json please. Also state your Symfony version. - Doug
@Doug I've added composer.json information. - Kafoso

5 Answers

1
votes

The problem is the autoload of symfony.

Open you composer.json file and edit :

AnnuaireBundle\AnnuaireBundle

"autoload": { "psr-4": { "AppBundle\": "src/AppBundle", "Kafoso\": "src/Kafoso" }, "classmap": [ "app/AppKernel.php", "app/AppCache.php" ] },

then, run the next command in your composer:

composer dumpautoload

1
votes

You can always rename every folder inside your on application project, you just have to alter the config files propery

Here are some hints:

  • Take a look at the config files in `app/config` if there is something old
  • Check all your `php-files` if the namespace is properly altered
  • Check your `project config-files` inside the Bundle `Resources/config`
  • Remove the `cache-folder` => `bin/cache` completely, symfony will automatically recreate it

And for the point of truth is saw your problem,... the error message is correct and pretty clear:

new Kafoso\TestApp\AppBundle\AppBundle()

for the registration the Bundle constructor must be called and this is located in the php file inside the folder which has the same name as the bundle-folder

... Since you added the composer.json, why don't you use:

"psr-0": { "": "src/" },

inside the autoload block

1
votes

I just had this issue in a project I maintain, and the problem was sensio_framework_extra bundle in app/config/config.yml:

I changed:

sensio_framework_extra:
        router:
            annotations: false 

To

sensio_framework_extra:
       router:
            annotations: true      <-----

All my routes where using annotations, so this was weird. Changed it and it started to work!

0
votes

Please in composer.json replace

"psr-4": {
"[Bundles names]\\": "src/[same bundle]"
// Even if there is one or more bundles

}, by this

"psr-4": {
"": "src/"

},

0
votes
file: config.yml 
fos_rest:
  routing_loader:
"If **enable:true** / Just change this for **false**. so if this is false, change this for true, and restart the project, i'm thinks this is a bug when we're upgrading to a new version!!, and i fixed in the way that I'm mentioning to you
"
    **enabled: false**