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.
Related
I want to use Bcrypt of Laminas-Crypt package in one of my files. After running
composer require laminas/laminas-crypt,
composer dump-autoload,
composer update
I declared a use statement in my controller:
use Laminas\Crypt\Password\Bcrypt;
The application loaded without any errors, but as far as I am concerned, all namespaces should be registered in config/modules.config.php for the autoloader to load the namespaces accordingly. After adding Laminas\Crypt namespace to the folder the application threw an exception:
Uncaught Laminas\ModuleManager\Exception\RuntimeException: Module (Laminas\Crypt) could not be initialized. in C:\xampp\htdocs\phpLessons\Shortly\vendor\laminas\laminas-modulemanager\src\ModuleManager.php:180
My question is:
Can the application function properly without the namespace of the used package being defined in the config/modules.config.php of root directory?
How can the exception be handled?
Edit:
Here is the relevant part of composer.json
"require": {
"php": ">=8.1.0",
"laminas/laminas-component-installer": "^3.0",
"laminas/laminas-development-mode": "^3.2",
"laminas/laminas-skeleton-installer": "^1.0",
"laminas/laminas-mvc": "^3.3.4",
"laminas/laminas-db": "^2.12.0",
"laminas/laminas-mvc-form": "^2.0.0",
"laminas/laminas-json": "^3.2",
"laminas/laminas-log": "^2.13.1",
"laminas/laminas-cli": "^1.1.1",
"laminas/laminas-mvc-i18n": "^1.2.0",
"laminas/laminas-mvc-plugins": "^1.1.0",
"laminas/laminas-mvc-middleware": "^2.0.0",
"laminas/laminas-session": "^2.10.0",
"laminas/laminas-di": "^3.2.2",
"laminas/laminas-view": "^2.25",
"laminas/laminas-form": "3.7",
"laminas/laminas-crypt": "^3.9",
"ext-pdo": "*",
"laminas/laminas-mail": "^2.20",
"doctrine/orm": "^2.14",
"doctrine/dbal": "^3.2",
"symfony/yaml": "^5.4",
"symfony/cache": "^5.4",
"doctrine/common": "^3.4",
"doctrine/doctrine-orm-module": "^5.3"
},
and modules.config.php from config/autoload:
return [
'Laminas\Cache',
'Laminas\Paginator',
'Laminas\Mail',
'Laminas\Mvc\Plugin\FilePrg',
'Laminas\Mvc\Plugin\FlashMessenger',
'Laminas\Mvc\Plugin\Identity',
'Laminas\Mvc\Plugin\Prg',
'Laminas\Session',
'Laminas\Mvc\Middleware',
'Laminas\Mvc\I18n',
'Laminas\Form',
'Laminas\I18n',
'Laminas\Log',
'Laminas\InputFilter',
'Laminas\Filter',
'Laminas\Hydrator',
'Laminas\Di',
'Laminas\Db',
'Laminas\Router',
'Laminas\Validator',
'Laminas\DeveloperTools',
'Laminas\Diactoros',
'DoctrineModule',
'DoctrineORMModule',
'Application',
'User',
'Pages',
];
I'm in deep over my head here trying to move into symfony flex finally. I'm following along with the symfonycast here (https://symfonycasts.com/screencast/symfony4-upgrade/flex-alive#play) moving step-by-step but I get an error where he has none and was wondering if I could get a hint as to where I've gone wrong.
upon running php bin/console I am hit by:
In FileLoader.php line 180:
There is no extension able to load the configuration for "when#dev" (in "/Users/mattias/Documents/www/webtools/config/package
s/monolog.yaml"). Looked for namespace "when#dev", found ""doctrine_cache", "doctrine", "fos_user", "sensio_framework_extra",
"monolog", "swiftmailer", "framework", "twig", "security"" in /Users/mattias/Documents/www/webtools/config/packages/monolog.
yaml (which is loaded in resource "/Users/mattias/Documents/www/webtools/config/packages/monolog.yaml").
In YamlFileLoader.php line 722:
There is no extension able to load the configuration for "when#dev" (in "/Users/mattias/Documents/www/webtools/config/package
s/monolog.yaml"). Looked for namespace "when#dev", found ""doctrine_cache", "doctrine", "fos_user", "sensio_framework_extra",
"monolog", "swiftmailer", "framework", "twig", "security"".
If I remove the when#dev from the monolog.yml file the same error appears with the next section instead (the when#test) so I assume there is some main wireing that hasn't been done etc.
I've googled the error but I seem to be the first one 😂
It seems as though I've ended up with monolog config for sym 5 so I wonder whether that tutorial is inaccurate in one of the composer directives. I might need to restrict some component to an earlier version when using it these days?
Here's my composer.json:
{
"name": "a/b",
"license": "proprietary",
"type": "project",
"autoload": {
"psr-4": {
"AppBundle\\": "src/AppBundle",
"App\\": "src/"
},
"classmap": [ "app/AppKernel.php", "app/AppCache.php" ]
},
"autoload-dev": {
"psr-4": {
"Tests\\AppBundle\\": "tests/AppBundle",
"App\\Tests\\": "tests/"
}
},
"repositories": [
{
"type": "package",
"package": {
"name": "jquery/jquery",
"version": "1.11.1",
"dist": {
"url": "https://code.jquery.com/jquery-1.11.1.js",
"type": "file"
}
}
}
],
"require": {
"php": "^7.1.3",
"doctrine/doctrine-bundle": "^1.6",
"doctrine/doctrine-cache-bundle": "^1.2",
"doctrine/orm": "^2.5",
"friendsofsymfony/user-bundle": "~2.1",
"sensio/framework-extra-bundle": "^5.0.0",
"symfony/console": "^4.0",
"symfony/flex": "^1.19",
"symfony/framework-bundle": "^4.0",
"symfony/lts": "^4#dev",
"symfony/monolog-bundle": "^3.1.0",
"symfony/polyfill-apcu": "^1.0",
"symfony/swiftmailer-bundle": "^3.1",
"symfony/yaml": "^4.0",
"twig/twig": "^1.0||^2.0"
},
"require-dev": {
"symfony/dotenv": "^4.0",
"symfony/phpunit-bridge": "^4.0"
},
"config": {
"preferred-install": {
"*": "dist"
},
"sort-packages": true,
"allow-plugins": {
"symfony/flex": true
}
},
"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": {
"id": "",
"allow-contrib": false
}
}
}
The configuration is not handled by the bundle (monolog in this case), but by flex itself.
After package installation it runs "recipes" that take care of creating configuration, assets, etc. You can find the recipe for the monolog version here. There's no when# directive to be found until 3.7 but composer.json declares ^3.1.0.
Use composer show symfony/monolog-bundle to see what version is installed, but I doubt there will be a problem in the version resolution. You can try to nuke vendor/ and reinstall all packages.
The easiest way to go about it is to delete config/packages/monolog.yaml. since you should have your old configuration under confib/packages/{dev,prod,test}/monolog.yaml everything should continue working. If that's not the case you can always reinstall the recipe with composer recipes:install symfony/monolog-bundle --force to bring it back.
As for the tutorial itself, on a brief glance everything seems okay so far.
And keep in mind that the ability to parse when directives comes from framework-bundle >= 5.3. If you plan to upgrade all the way up you might want to reinstall recipes to pull in the latests configs (or maybe use composer recipes:update for an interactive, git-based update). Having all configs in one file is only a matter of convenience if the changes are small per environment, but everything will work just the same.
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.
I'm new in composer but I could get vendor, composer.lock and composer.json from cmd then I paste them to my project without Validation_Master folder.
My project Path: C:\wamp64\www\php Projects\project 1\(here there are test.php and vendor, composer.lock and composer.json)
Validation_Master folder path: C:\wamp64\www\php Projects\Validation_Master
My Code is:
<?php
require "vendor/autoload.php";
use Respect\Validation\Validator as v;
$number = 123;
v::numeric()->validate($number);
?>
But the above code gives me the following error :
Fatal error: Class 'Respect\Validation\Validator' not found in
C:\wamp64\www\php Projects\project 1\test.php
What did I do?!!?
copy Validation_Master folder to the path of test.php ==> (Failed )
copy Vendor folder to the path of Validation_Master folder and required from there ==> (Failed )
Manipulate namespace a lot and add namespace Respect\Validation to the code ==> (Failed )
What's The PROBLEM ????
How Can I use Respect Validation Library ????? Please answer me basically (I read docs before but didn't help)
vendor folder image
composer.json code:
{
"name": "respect/validation",
"description": "The most awesome validation engine ever created for PHP",
"keywords": ["respect", "validation", "validator"],
"type": "library",
"homepage": "http://respect.github.io/Validation/",
"license": "BSD Style",
"authors": [
{
"name": "Respect/Validation Contributors",
"homepage": "https://github.com/Respect/Validation/graphs/contributors"
}
],
"require": {
"php": ">=5.6"
},
"require-dev": {
"egulias/email-validator": "~1.2",
"malkusch/bav": "~1.0",
"mikey179/vfsStream": "^1.5",
"phpunit/phpunit": "~5.3",
"symfony/validator": "~2.6.9",
"zendframework/zend-validator": "~2.3"
},
"suggest": {
"ext-bcmath": "Arbitrary Precision Mathematics",
"ext-mbstring": "Multibyte String Functions",
"egulias/email-validator": "Strict (RFC compliant) email validation",
"malkusch/bav": "German bank account validation",
"symfony/validator": "Use Symfony validator through Respect\\Validation",
"zendframework/zend-validator": "Use Zend Framework validator through Respect\\Validation",
"fabpot/php-cs-fixer": "Fix PSR2 and other coding style issues"
},
"autoload": {
"psr-4": {
"Respect\\Validation\\": "library/"
}
},
"autoload-dev": {
"psr-4": {
"Respect\\Validation\\": "tests/library/"
}
},
"extra": {
"branch-alias": {
"dev-master": "2.0-dev"
}
},
"scripts": {
"test": "./vendor/bin/phpunit"
}
}
the autoload.php code :
<?php
require_once __DIR__ . '/composer' . '/autoload_real.php';
return ComposerAutoloaderInit46e0d859a60be6f2acf30ed92a2228ad::getLoader();
I appreciate and sorry that I've written a lot
UPDATE : My problem like this post : Why my autoload.php of composer doesn't work?, but it didn't help
I also run composer dump-autoload in cmd ==> (failed)
It looks to me like you copied the composer.json of the respect/validation library, and tried to use it as the composer.json for your project. This is a completely wrong approach.
Use this as your project composer.json:
{
"require": {
"respect/validation": "^1.0"
}
}
Then:
run composer install,
run your script again.
I upgraded a project from Symfony 2.0 to 2.1. After that my tests stopped working. The problem is that I use fixtures which implement the ContainerAware interface. Now after the upgrade the setContainer() method does not get called anymore. I tried upgrading further to 2.2. The problem still persists.
composer.json:
{
"name": "symfony/framework-standard-edition",
"license": "MIT",
"type": "project",
"description": "The \"Symfony Standard Edition\" distribution",
"autoload": {
"psr-0": { "": "src/" }
},
"require": {
"php": ">=5.3.3",
"symfony/symfony": "2.2.*",
"doctrine/orm": ">=2.2.3,<2.5-dev",
"doctrine/doctrine-bundle": "1.2.*",
"twig/extensions": "1.0.*#dev",
"symfony/assetic-bundle": "2.1.*",
"symfony/swiftmailer-bundle": "2.2.*",
"symfony/monolog-bundle": "2.2.*",
"sensio/distribution-bundle": "2.2.*",
"sensio/framework-extra-bundle": "2.2.*",
"sensio/generator-bundle": "2.2.*",
"jms/security-extra-bundle": "1.4.*",
"jms/di-extra-bundle": "1.3.*",
"kriswallsmith/assetic": "1.1.*#dev",
"knplabs/knp-menu-bundle":"dev-master",
"knplabs/knp-menu":"dev-master",
"doctrine/doctrine-fixtures-bundle": "dev-master",
"doctrine/data-fixtures": "1.0.*#ALPHA",
"doctrine/migrations": "dev-master",
"doctrine/doctrine-migrations-bundle": "dev-master",
"jms/translation-bundle": "dev-master"
},
"scripts": {
"post-install-cmd": [
"Sensio\\Bundle\\DistributionBundle\\Composer\\ScriptHandler::buildBootstrap",
"Sensio\\Bundle\\DistributionBundle\\Composer\\ScriptHandler::clearCache",
"Sensio\\Bundle\\DistributionBundle\\Composer\\ScriptHandler::installAssets",
"Sensio\\Bundle\\DistributionBundle\\Composer\\ScriptHandler::installRequirementsFile"
],
"post-update-cmd": [
"Sensio\\Bundle\\DistributionBundle\\Composer\\ScriptHandler::buildBootstrap",
"Sensio\\Bundle\\DistributionBundle\\Composer\\ScriptHandler::clearCache",
"Sensio\\Bundle\\DistributionBundle\\Composer\\ScriptHandler::installAssets",
"Sensio\\Bundle\\DistributionBundle\\Composer\\ScriptHandler::installRequirementsFile"
]
},
"extra": {
"symfony-app-dir": "app",
"symfony-web-dir": "web",
"branch-alias": {
"dev-master": "2.2-dev"
}
}
}
and my fixture looks like this:
LoadUserData.php
<?php
namespace FINDOLOGIC\CustomerLoginBundle\DataFixtures\ORM;
use Doctrine\Common\Persistence\ObjectManager;
use Doctrine\Common\DataFixtures\FixtureInterface;
use Symfony\Component\DependencyInjection\ContainerAwareInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
class LoadUserData implements FixtureInterface, ContainerAwareInterface
{
/**
* #var ContainerInterface
*/
private $container;
public function setContainer(ContainerInterface $container = null)
{
$this->container = $container;
}
public function load(ObjectManager $manager)
{
[...]
}
}
I've already taken a look at Doctrine fixtures not working after updating to Symfony 2.1.8 but it does not solve my problem.
I'm really looking forward to your input as I have been trying to solve this for quite a while now.
I had exactly the same issue, nothing would fix it via changing round composer order, order in the kernal, extending or implementing etc, so in the end, I just pass the container in from the test:
public function testLogin()
{
///
$fixture = new UserFixtures();
$fixture->setContainer($container);
///
$executor->execute($loader->getFixtures(), true);
And now it works fine.
Solution:
Make sure you not only implement ContainerAwareInterface - you should extend ContainerAware
use Symfony\Component\DependencyInjection\ContainerAware;
// ContainerAware... already implements ContainerAwareInterface
class YourFixture extends ContainerAware
{
// ...
Another suggestion:
Move Datafixtures further up in your composer.json ( before doctrine/orm ) to have composer generate the classmap correctly.
The 2.0 documentation states.
Be sure to register the new namespace before Doctrine\Common.
Otherwise, Symfony will look for data fixture classes inside the
Doctrine\Common directory. Symfony's autoloader always looks for a
class inside the directory of the first matching namespace, so more
specific namespaces should always come first.
Furthermore you don't need fixtures-bundle and fixtures as fixtures is a dependency of fixtures-bundle.
Hope that helps.