Deploying a Symfony app - ClassNotFoundException Error - php

I am learning the Symfony framework and trying to deploy a simple boilerplate app I put together to Heroku using Git.
The deployment fails due to the following fatal error:
Uncaught Symfony\Component\Debug\Exception\ClassNotFoundException: Attempted to load class "WebServerBundle" from namespace "Symfony\Bundle\WebServerBundle
Did you forget a "use" statement for another namespace? in /tmp/build_cbeb92af6c9ee04b07e1f85618211649/src/Kernel.php:32
Kernel.php
<?php
namespace App;
use Symfony\Bundle\FrameworkBundle\Kernel\MicroKernelTrait;
use Symfony\Component\Config\Loader\LoaderInterface;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\HttpKernel\Kernel as BaseKernel;
use Symfony\Component\Routing\RouteCollectionBuilder;
class Kernel extends BaseKernel {
use MicroKernelTrait;
const CONFIG_EXTS = '.{php,xml,yaml,yml}';
public function getCacheDir(){
return $this->getProjectDir().'/var/cache/'.$this->environment;
}
public function getLogDir(){
return $this->getProjectDir().'/var/log';
}
public function registerBundles(){
$contents = require $this->getProjectDir().'/config/bundles.php';
foreach ($contents as $class => $envs) {
if (isset($envs['all']) || isset($envs[$this->environment])) {
yield new $class();
}
}
}
protected function configureContainer(ContainerBuilder $container, LoaderInterface $loader){
$container->setParameter('container.autowiring.strict_mode', true);
$container->setParameter('container.dumper.inline_class_loader', true);
$confDir = $this->getProjectDir().'/config';
$loader->load($confDir.'/packages/*'.self::CONFIG_EXTS, 'glob');
if (is_dir($confDir.'/packages/'.$this->environment)) {
$loader->load($confDir.'/packages/'.$this->environment.'/**/*'.self::CONFIG_EXTS, 'glob');
}
$loader->load($confDir.'/services'.self::CONFIG_EXTS, 'glob');
$loader->load($confDir.'/services_'.$this->environment.self::CONFIG_EXTS, 'glob');
}
protected function configureRoutes(RouteCollectionBuilder $routes){
$confDir = $this->getProjectDir().'/config';
if (is_dir($confDir.'/routes/')) {
$routes->import($confDir.'/routes/*'.self::CONFIG_EXTS, '/', 'glob');
}
if (is_dir($confDir.'/routes/'.$this->environment)) {
$routes->import($confDir.'/routes/'.$this->environment.'/**/*'.self::CONFIG_EXTS, '/', 'glob');
}
$routes->import($confDir.'/routes'.self::CONFIG_EXTS, '/', 'glob');
}
}
What Ive done so far:
install the dotenv bundle (composer require symfony/dotenv)
run composer dump-autoload
set the production environment variable via the heroku CLI as per this article (heroku config:set SYMFONY_ENV=prod)
Some things Ive learned/have noticed:
According to the README file for WebServerBundle:
WebServerBundle provides commands for running applications using the PHP
built-in web server. It simplifies your local development setup because you
don't have to configure a proper web server such as Apache or Nginx to run your
application.
.. that is, WebServerBundle is a development dependency - yet it is being included in production.
WebServerBundle is also being included in the composer.lock file under autoload
"autoload": {
"psr-4": {
"Symfony\\Bundle\\WebServerBundle\\": ""
},
...
},
My bundles.php file includes WebServerBundle for dev
return [
Symfony\Bundle\FrameworkBundle\FrameworkBundle::class => ['all' => true],
Symfony\Bundle\WebServerBundle\WebServerBundle::class => ['dev' => true],
Symfony\Bundle\WebProfilerBundle\WebProfilerBundle::class => ['dev' => true, 'test' => true],
Symfony\Bundle\TwigBundle\TwigBundle::class => ['all' => true],
Sensio\Bundle\FrameworkExtraBundle\SensioFrameworkExtraBundle::class => ['all' => true],
];
I have a .env file and a .env.dist file - Im assuming the .env.dist file is for production? They both have the same contents:
APP_ENV=dev
APP_SECRET=0473d15a4ce2723619d2e8b0405186d3
Im pretty new to symfony and do not really understand how a production environment is instantiated other than that the dotenv bundle reads the .env file and sets the environment variables.
Any help and clarity on all of this would be appreciated.
Edit: here is my composer.json file
{
"type": "project",
"license": "proprietary",
"require": {
"php": "^7.1.3",
"ext-iconv": "*",
"sensio/framework-extra-bundle": "^5.1",
"symfony/asset": "^4.0",
"symfony/console": "^4.0",
"symfony/dotenv": "^4.0",
"symfony/flex": "^1.0",
"symfony/framework-bundle": "^4.0",
"symfony/lts": "^4#dev",
"symfony/twig-bundle": "^4.0",
"symfony/yaml": "^4.0"
},
"require-dev": {
"symfony/profiler-pack": "^1.0",
"symfony/web-server-bundle": "^4.0"
},
"config": {
"preferred-install": {
"*": "dist"
},
"sort-packages": true
},
"autoload": {
"psr-4": {
"App\\": "src/"
}
},
"autoload-dev": {
"psr-4": {
"App\\Tests\\": "tests/"
}
},
"replace": {
"symfony/polyfill-apcu": "*",
"symfony/polyfill-php70": "*",
"symfony/polyfill-php56": "*"
},
"scripts": {
"auto-scripts": {
"cache:clear": "symfony-cmd",
"assets:install --symlink --relative %PUBLIC_DIR%": "symfony-cmd"
},
"post-install-cmd": [
"#auto-scripts"
],
"post-update-cmd": [
"#auto-scripts"
]
},
"conflict": {
"symfony/symfony": "*"
},
"extra": {
"symfony": {
"id": "01C1BVQJ19BG3WAS3299PDHH9P",
"allow-contrib": false
}
}
}

I'm not sure about the command you are using, (I use git)
heroku config:set SYMFONY_ENV=prod
But if your .env file has
APP_ENV=dev
That is explictly setting the environment to dev, so it is trying to load dev dependencies (proven by your error) which don't get pushed to the server(read the article you provided).
You need the .env file that is on the server to have APP_ENV=prod
The .env is for the specific machine, it is ignored by git, while .env.dist is tracked. So edit the .env.dist and commit it, then once it is on the server just rename it .env then I would run composer install or composer update on the server which will update dependencies and clear the cache. Then refresh your browser.

The answer likely depends on how you deploy your app and I assume you will install only production dependencies in your deployment, i.e. you run composer with --no-dev as you should. This means that none of the dev dependencies, which very likely includes the webserver-bundle as this is not needed in production where you use a real webserver like apache or nginx, are installed.
What it boils down to is either your code requiring this dev dependency which is not installed or your webserver is trying to run your app in the wrong APP_ENV.
If it's the latter you should check your webserver config if you set the appropriate environment variables (APP_ENV & APP_DEBUG at the very least, probably also the DATABASE_URL). In apache you have to use SetEnv APP_ENV prod and in nginx you use fastcgi_param APP_ENV prod.
edit: I just read that you use composer, so dev dependencies are definitely not installed as is mentioned in the Getting Started Guide: https://devcenter.heroku.com/articles/php-support#build-behavior
So you can't use the dev environment for Symfony or you have to reinstall the dev-dependencies as regular dependencies using composer (which is absolutely not recommended).

Related

cant install cakephp 4 again

I have wamp64, php 7.4 and using composer I ran this from the console
composer create-project --prefer-dist "cakephp/app:^4.0" myapp
and
composer create-project --prefer-dist cakephp/app:~4.0 my_app_name
The installation cut out towards the end with this error
Script App\Console\Installer::postInstall handing the post create project cmd event terminated with an exception
[Symfony\Component\Console\Exception\RunTimeException\ Aborted
and some Symfony runtime exception when you get to the set folder permission y/n
It created this JSON file
{
"name": "cakephp/app",
"description": "CakePHP skeleton app",
"homepage": "https://cakephp.org",
"type": "project",
"license": "MIT",
"require": {
"php": ">=7.2",
"cakephp/cakephp": "~4.2.0",
"cakephp/migrations": "^3.0",
"cakephp/plugin-installer": "^1.3",
"mobiledetect/mobiledetectlib": "^2.8"
},
"require-dev": {
"cakephp/bake": "^2.3",
"cakephp/cakephp-codesniffer": "~4.2.0",
"cakephp/debug_kit": "^4.4",
"josegonzalez/dotenv": "^3.2",
"phpunit/phpunit": "~8.5.0 || ^9.3",
"psy/psysh": "#stable"
},
"suggest": {
"markstory/asset_compress": "An asset compression plugin which provides file concatenation and a flexible filter system for preprocessing and minification.",
"dereuromark/cakephp-ide-helper": "After baking your code, this keeps your annotations in sync with the code evolving from there on for maximum IDE and PHPStan/Psalm compatibility.",
"phpstan/phpstan": "PHPStan focuses on finding errors in your code without actually running it. It catches whole classes of bugs even before you write tests for the code."
},
"autoload": {
"psr-4": {
"App\\": "src/"
}
},
"autoload-dev": {
"psr-4": {
"App\\Test\\": "tests/",
"Cake\\Test\\": "vendor/cakephp/cakephp/tests/"
}
},
"scripts": {
"post-install-cmd": "App\\Console\\Installer::postInstall",
"post-create-project-cmd": "App\\Console\\Installer::postInstall",
"check": [
"#test",
"#cs-check"
],
"cs-check": "phpcs --colors -p src/ tests/",
"cs-fix": "phpcbf --colors -p src/ tests/",
"stan": "phpstan analyse",
"test": "phpunit --colors=always"
},
"prefer-stable": true,
"config": {
"sort-packages": true
}
}
How to Fix Cakephp 4 Composer Install Error
(i didnt understand the solution to this)
Cakephp 4 Windows Installation Issues
It should be possible to get it to work. What Windows version do you use? Pro or Home? Which distribution? 2010?
Please make sure to navigate to the WAMP install folder and then specifically the www folder.
The command I use is this;
composer create-project --prefer-dist cakephp/app:~4.0 cake
It worked for me.
Alternatively, download Cakephp version 4.0, place the contents in the www folder, and configure it manually through the file /config/app.php. If you want, we can look at it together. There are not many people around me that can or want to work on Cakephp, so it'd be a pleasure. :)

Understanding Symfony/mailer: why can't I found Symfony\Contracts\HttpClient\HttpClientInterface in Vendors?

I'm trying to figure out something I couldn't understand while exploring Symfony/Mailer source code.
At first I created a whole new project using these commands:
symfony new my_project_name --version=lts
composer require symfony/mailer
Then, when I was exploring the Symfony\Component\Mailer\Transport\AbstractHttpTransport and saw
it was using Symfony\Contracts\HttpClient\HttpClientInterface.
I was surprised to find out that the HttpClientInterface is not part of my project, I can't find it anywhere and neither does PhpStorm.
How is this possible? Is that a programming trick I am not aware of?
I searched on the internet and find out about Symfony/Contracts project and its purpose but I don't understand how it can be declared in the project and not being part of it.
Here is a part of the source code of the AbstractHttpTransport in Symfony/mailer
use Symfony\Contracts\HttpClient\HttpClientInterface;
...
abstract class AbstractHttpTransport extends AbstractTransport
{
public function __construct(HttpClientInterface $client = null, EventDispatcherInterface $dispatcher = null, LoggerInterface $logger = null)
...
Edit: As required here is the composer.json generated for the project
{
"type": "project",
"license": "proprietary",
"require": {
"php": "^7.1.3",
"ext-ctype": "*",
"ext-iconv": "*",
"symfony/console": "4.4.*",
"symfony/dotenv": "4.4.*",
"symfony/flex": "^1.3.1",
"symfony/framework-bundle": "4.4.*",
"symfony/mailer": "4.4.*",
"symfony/yaml": "4.4.*"
},
"require-dev": {
},
"config": {
"preferred-install": {
"*": "dist"
},
"sort-packages": true
},
"autoload": {
"psr-4": {
"App\\": "src/"
}
},
"autoload-dev": {
"psr-4": {
"App\\Tests\\": "tests/"
}
},
"replace": {
"paragonie/random_compat": "2.*",
"symfony/polyfill-ctype": "*",
"symfony/polyfill-iconv": "*",
"symfony/polyfill-php71": "*",
"symfony/polyfill-php70": "*",
"symfony/polyfill-php56": "*"
},
"scripts": {
"auto-scripts": {
"cache:clear": "symfony-cmd",
"assets:install %PUBLIC_DIR%": "symfony-cmd"
},
"post-install-cmd": [
"#auto-scripts"
],
"post-update-cmd": [
"#auto-scripts"
]
},
"conflict": {
"symfony/symfony": "*"
},
"extra": {
"symfony": {
"allow-contrib": false,
"require": "4.4.*"
}
}
}
The reason why you are unable to find HttpClientInterface, is that you do not require any library that provides a symfony/http-client-implementation.
To allow a transport to use symfony/http-client you will need to explicitly install and configure it.
composer require symfony/http-client
# config/packages/framework.yaml
framework:
# ...
http_client:
max_host_connections: 10
default_options:
headers: { 'X-Powered-By': 'ACME App' }
max_redirects: 7
I don't understand how it can be declared in the project and not being
part of it.
You are able to declare any fully qualified namespace within a PHP application, without the subsequent class or file existing. Example: https://3v4l.org/RIWqS
namespace Foo;
use Non\Existing\NamespaceClass;
class Bar
{
public function __construct()
{
echo NamespaceClass::class;
}
}
new \Foo\Bar(); //Non\Existing\NamespaceClass
Symfony has the capability to ignore autowired arguments that declare a service namespace that does not exist, and will instead supply null for its value.
Clarification
symfony/mailer is a modular component, that does not require the Symfony Framework or the symfony/http-client in order to be used and can be used in virtually any application. Since the mailer can support a variety of different transports and those transports different clients, the code is included to work with them, but they are not a dependency for the mailer package. Allowing you to choose the transports you want to use.
In this instance, extending the AbstractHttpTransport will also require a service that implements the Symfony\Contracts\HttpClient\HttpClientInterface to be provided or overridden. This allows for the HttpClientInterface to be used when it is available.
Symfony Framework specifically looks for the configuration for http_client and will configure the services that can utilize it for you. [sic] Once symfony/http-client is installed and configured, a transport that supports it, will utilize it for the connection.
Source
The HttpClientInterface is supplied in symfony/http-client-contracts. Where a library that provides the implementation, will specify as such in the composer.json as "provide": { "symfony/http-client-implementation": "*" }. Such as symfony/http-client
Which is why no classes within symfony/mailer package extends AbstractHttpTransport, as it is intended to be used by other libraries, such as symfony/amazon-mailer. While symfony/mailer does not provide the implementation for symfony/http-client, multiple classes and methods reference HttpClientInterface, allowing it to be used when available.
For more information see Symfony Contracts and Symfony Http Client
Usage
The various transports that symfony/mailer supports need to be added as desired by you. By default the only transport symfony/mailer comes pre-installed with is SMTP. Where other transports that support the symfony/http-client features but do not require it, are also available.

"Really" changing php version in PhpStorm (not Settings)

I created a php project (by cmd >composer create-project symfony/framework-standard-edition projectname) which I initially assigned the 5.6 php version. Later, in PhpStorm I decided to change it to the 7.1 thourgh Settings/Languages & Frameworks/PHP...
The problems is that when I installed PhpUnit (7.1 version) in that project, I got the compatibility error of that my php version was 5.6
Understanding that the "Settings way" dinĀ“t work, I went to the composer.json, when I though there is the problem:
{
"name": "symfony/framework-standard-edition",
"license": "MIT",
"type": "project",
"description": "The \"Symfony Standard Edition\" distribution",
"autoload": {
"psr-4": {
"AppBundle\\": "src/AppBundle"
},
"classmap": [ "app/AppKernel.php", "app/AppCache.php" ]
},
"autoload-dev": {
"psr-4": { "Tests\\": "tests/" },
"files": [ "vendor/symfony/symfony/src/Symfony/Component/VarDumper/Resources/functions/dump.php" ]
},
"require": {
"php": ">=5.5.9",
"doctrine/doctrine-bundle": "^1.6",
"doctrine/orm": "^2.5",
"incenteev/composer-parameter-handler": "^2.0",
"phpunit/phpunit": "5.6.0",
"sensio/distribution-bundle": "^5.0.19",
"sensio/framework-extra-bundle": "^5.0.0",
"symfony/monolog-bundle": "^3.1.0",
"symfony/polyfill-apcu": "^1.0",
"symfony/swiftmailer-bundle": "^2.6.4",
"symfony/symfony": "3.4.*",
"twig/twig": "^1.0||^2.0"
},
"require-dev": {
"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": {
"platform": {
"php": "5.6"
},
"sort-packages": true
},
"extra": {
"symfony-app-dir": "app",
"symfony-bin-dir": "bin",
"symfony-var-dir": "var",
"symfony-web-dir": "web",
"symfony-tests-dir": "tests",
"symfony-assets-install": "relative",
"incenteev-parameters": {
"file": "app/config/parameters.yml"
},
"branch-alias": {
"dev-master": "3.4-dev"
}
}
}
My question is: which specific lines must I change and if there are another place/s of the project to look for.
Edit:
After changing the php versions in
"config": {
"platform": {
"php": "7.1"
},
and
"require": {
"php": "^7.1",
I've executed the composer update and although this time I don't get the previous error, it appears instead another new one:
Symfony\Component\Debug\Exception\FatalThrowableError: Parse error: syntax error, unexpected ')' in (file route) on line 25
But I don't see why the line with the code return $this->render(...); is incorrect since my php file is based on the official tutorial https://symfony.com/doc/3.4/email.html
<?php
namespace AppBundle\Controller;
use Symfony\Component\HttpKernel\Tests\Controller;
class SendCustomerEmailController extends Controller
{
public function indexAction($name, $email, $originEmail, \Swift_Mailer $mailer)
{
$message = (new \Swift_Message('Pago reserva'))
->setFrom($originEmail)
->setTo($email)
->setBody(
$this->renderView(
'emails/email-template.html.twig',
array('name' => $name)
),
'text/html'
);
$mailer->send($message);
return $this->render(...);
}
}
If the syntax is incorrect, why didn't PhpStorm warned me?
You should install php7.1 to your system first to be able to use it.
For ubuntu:
sudo add-apt-repository ppa:ondrej/php
sudo apt-get update
sudo apt-get install php7.1
For windows and other OS just google it (e.g. How can I install php on Windows).
Then you should say PHPUnit to use specific version of php, but it depends on how exactly you run tests.
See Run test in phpunit with specific php version
or
PHPUnit - This version of PHPUnit requires PHP 5.6; using the latest version of PHP is highly recommended - OSX 10.11
You should install PHP 7.1 if you using PhpUnit 7.1
Change this line
"php": ">=5.5.9"
To
"php": "^7.1"
After that execute composer update
You have the wrong syntax for rendering :
$this->render(...);
You should specify a template or calling a custom Twig function
https://symfony.com/doc/current/templating/embedding_controllers.html
Something like that :
return $this->render('folder/exemple.html.twig');
Its is possible that you are using the wrong library? I just take a look in my controllers and the class whateverController extends Controller. This "Controller" comes from the line "use Symfony\Bundle\FrameworkBundle\Controller\Controller;"
Try to change your:
use Symfony\Component\HttpKernel\Tests\Controller;
for:
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
In PHPStorm under "Settings" - "Languages & Frameworks" - "PHP" you have two dropdown selections:
"PHP language level"
"CLI interpreter"
Changing the former will not necessarily change the latter. But the CLI interpreter is the PHP version being used for Composer and test execution. If you have PHP 5.6 there, Composer will not properly update dependencies, and tests will fail because PHP 7.1 syntax cannot be executed by PHP 5.6.
Install PHP 7.1, then add the CLI interpreter (the three dots at the end of that dropdown).
Hint: You can have more than one PHP version installed in PHPStorm if you know how to install to different directories. It's easy on Windows (you simply download a version and unzip, then point PHPStorm to the php.exe inside), and may be more complicated on Linux.

Symfony: Namespaced bundle not loaded correctly

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.
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
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
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!
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/"
},
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**

Changing namespace on my controller - Silex framework

I'll go straight to the point, I only changed the controller namespace Musique\Controller to STM\Controller
namespace STM\Controller;
use Silex\Application;
class HomeController{
public function indexAction(Application $app){
return $app['twig']->render("index.html.twig");
}
also changed my composer.json file
{
"name": "jasonazoulay/SoTrustMe",
"description": "Tiers de confiance",
"authors": [
{
"name": "Jason Azoulay",
}
],
"require": {
"silex/silex": "^2.0",
"doctrine/dbal": "~2.5",
"twig/twig": "^1.33",
"symfony/asset": "^3.2",
"symfony/twig-bridge": "^3.2",
"symfony/form": "~2.8|3.0.*",
"symfony/translation": "~2.8|3.0.*",
"symfony/config": "~2.8|3.0.*"
},
"autoload": {
"psr-4": {"SoTrustMe\\": "src"}
}
}
and of course the route also
$app->get('/',"STM\Controller\HomeController::indexAction")->bind('home');
and now i get this error
InvalidArgumentException in ControllerResolver.php line 187:
Class "STM\Controller\HomeController" does not exist.
though it was working just fine before I change the namespace !
please help me
Since you are loading your classes with composer and you introduced a new directory in your project you are going to need to re-run: composer dump-autoload
this will regenerate the list of all classes that need to be included in the project and updates the Composer cache. For more detail regarding this issue you could have a look at this question.

Categories