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.
composer.json
information. - Kafoso