I'm struggling with the error:
The autoloader expected class "ElectricAnimal\CardsinthepostBundle\Services\MyService" to be
defined in file
"/vhosts/domains/cardsinthepost.com/public/app/../src/ElectricAnimal/CardsinthepostBundle/Services/MyService.php".
You probably have a typo in the namespace or the class name.
But that class is defined in exactly that file!
/src/ElectricAnimal/CardsinthepostBundle/Services/MyService.php:
<?php
// Bundle/ElectricAnimalCardsinthepost/Services/MyService.php
namespace Bundle\ElectricAnimalCardsinthepost\Services;
class MyService
{
public function __construct()
{
}
public function sum($n1, $n2) {
return $n1 + $n2;
}
}
In my controller I have:
<?php
class DefaultController extends Controller
{
public function indexAction()
{
$number = $this->get('my_service')->sum(12, 37);
return new Response('<pre>' . $number . '</pre>');
}
}
?>
Additional info:
/src/ElectricAnimal/CardsinthepostBundle/DependencyInjection/ElectricAnimalCardsinthepostExtension.php:
<?php
namespace ElectricAnimal\CardsinthepostBundle\DependencyInjection;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\Config\FileLocator;
use Symfony\Component\HttpKernel\DependencyInjection\Extension;
use Symfony\Component\DependencyInjection\Loader;
/**
* This is the class that loads and manages your bundle configuration
*
* To learn more see {#link http://symfony.com/doc/current/cookbook/bundles/extension.html}
*/
class ElectricAnimalCardsinthepostExtension extends Extension
{
/**
* {#inheritDoc}
*/
public function load(array $configs, ContainerBuilder $container)
{
$configuration = new Configuration();
$config = $this->processConfiguration($configuration, $configs);
$loader = new Loader\YamlFileLoader($container, new FileLocator(__DIR__.'/../Resources/config'));
$loader->load('services.yml');
}
}
/src/ElectricAnimal/CardsinthepostBundle/Resources/config/services.yml:
services:
my_service:
class: ElectricAnimal\CardsinthepostBundle\Services\MyService
My answer:
MyService.php was using slightly the wrong namespace. Needed:
// Bundle/ElectricAnimal/Cardsinthepost/Services/MyService.php
namespace ElectricAnimal\CardsinthepostBundle\Services;
instead of
// Bundle/ElectricAnimalCardsinthepost/Services/MyService.php
namespace Bundle\ElectricAnimalCardsinthepost\Services;
Symfony2 naming drives me round the bend sometimes!
Had already constructed question so hope useful for someone to have this answer.
Related
I trying to load the api_platform.iri_converter but get an error:
The \"api_platform.iri_converter\" service or alias has been removed or inlined when the container was compiled. You should either make it public, or stop using the container directly and use dependency injection instead.
This is the code:
declare(strict_types=1);
namespace App\Security\Authorization\Voter;
use Symfony\Component\DependencyInjection\ContainerInterface;
abstract class BaseVoter extends Voter
{
public ContainerInterface $container;
public function __construct(ContainerInterface $container)
{
$this->container = $container;
}
}
declare(strict_types=1);
namespace App\Security\Authorization\Voter;
class VenueVoter extends BaseVoter
{
protected function voteOnAttribute(): bool
{
/** #var User $tokenUser */
$tokenUser = $token->getUser();
if (self::VENUE_CREATE === $attribute) {
$iri = $this->container->get('api_platform.iri_converter')->getItemFromIri($valueWithIri);
}
}
}
Do not inject the Container.
Instead, inject the IriConverter directly.
use ApiPlatform\Core\Bridge\Symfony\Routing\IriConverterInterface;
abstract class BaseVoter extends Voter
{
public IriConverterInterface $iriConverter;
public function __construct(IriConverterInterface $iriConverter)
{
$this->iriConverter = $iriConverter;
}
}
I'm trying to create an image fixture for SonataMediaBundle. The media is saved in the database and shown in the admin interface, but the image is missing. There is no difference between the records created by fixture and admin interface.
Here is my Fixture in AppBundle\DataFixtures\ORM\LoadMediaBundleMediaData
<?php
namespace AppBundle\DataFixtures\ORM;
use Doctrine\Common\DataFixtures\AbstractFixture;
use Doctrine\Common\DataFixtures\OrderedFixtureInterface;
use Symfony\Component\DependencyInjection\ContainerAwareInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Doctrine\Common\Persistence\ObjectManager;
use Symfony\Component\HttpFoundation\File\File;
use Application\Sonata\ClassificationBundle\Entity\Context;
use Application\Sonata\ClassificationBundle\Entity\Category;
class LoadMediaBundleMediaData extends AbstractFixture implements OrderedFixtureInterface, ContainerAwareInterface
{
/**
* #var ContainerInterface
*/
private $container;
/**
* {#inheritDoc}
*/
public function load(ObjectManager $manager)
{
$context = new Context();
$context->setId('default');
$context->setName('default');
$context->setEnabled(true);
$manager->persist($context);
$manager->flush();
$category = new Category();
$category->setName('Default');
$category->setContext($context);
$category->setEnabled(true);
$manager->persist($category);
$manager->flush();
$media1File = new File(__DIR__ . '/../Assets/Images/image.jpg');
$mediaManager = $this->container->get('sonata.media.manager.media');
$media1 = $mediaManager->create();
$media1->setEnabled(true);
$media1->setBinaryContent($media1File);
$media1->setName('brunnkogel.jpg');
$media1->setContext('default');
$media1->setProviderName('sonata.media.provider.image');
$media1->setProviderStatus(1);
$mediaManager->save($media1);
$manager->persist($media1);
$manager->flush();
}
public function setContainer(ContainerInterface $container = null)
{
$this->container = $container;
}
/**
* {#inheritDoc}
*/
public function getOrder()
{
return 40;
}
}
If I create a media in the admin interface it works and the image is saved at web/uploads/media/default/0001/01/c4412442f8f146db6acaec10751fb54e4c80e8c5.jpeg. Also the thumbnails are in this directory. If I create the media by fixtures, the files in web/uploads/media are missing.
I'm using Symfony 2.8 and SonataMediaBundle 3.0.
How can I solve this issue?
i have an error starting with Symfony2.
InvalidArgumentException in YamlFileLoader.php line 356:
There is no extension able to load the configuration for "services" (in /Users/jm/Documents/Websites/www.brsymfony.dev/src/CalcBundle/DependencyInjection/../Resources/config/services.yml). Looked for namespace "services", found none
My services.yml look like and it has whitespaces correctly:
services:
Calculator:
class: CalcBundle\Services\Calculator
arguments:
- #doctrine.orm.default_entity_manager
ListOperations:
class: CalcBundle\Services\ListOperations
arguments:
- #doctrine.orm.default_entity_manager
Can anyone help me?
--
Yes, i have this code on CalcExtension.php inside DependencyInjection folder:
<?php
namespace CalcBundle\DependencyInjection;
use Symfony\Component\Config\FileLocator;
use Symfony\Component\DependencyInjection\Loader\YamlFileLoader;
use Symfony\Component\HttpKernel\DependencyInjection\Extension;
use Symfony\Component\DependencyInjection\ContainerBuilder;
/**
* Class AppExtension
*/
class CalcExtension extends Extension
{
/**
* Loads a specific configuration.
*
* #param array $config An array of configuration values
* #param ContainerBuilder $container A ContainerBuilder instance
*
* #throws \InvalidArgumentException When provided tag is not defined in this extension
*
* #api
*/
public function load(array $config, ContainerBuilder $container)
{
$loader = new YamlFileLoader(
$container,
new FileLocator(__DIR__ . '/../Resources/config')
);
$loader->load('services.yml');
}
}
Thanks in advance :)
Have you created the extension on the DependencyInjection folder? Should be CalcBundle/DependencyInjection/CalcExtension.php
You have the documentation with examples here
http://symfony.com/doc/current/cookbook/bundles/extension.html
public function load(array $configs, ContainerBuilder $container)
{
$configuration = new Configuration();
$config = $this->processConfiguration($configuration, $configs);
$loader = new Loader\YamlFileLoader($container, new FileLocator(__DIR__.'/../Resources/config'));
$loader->load('services.yml');
}
The services.yml should be located on CalcBundle/Resources/config/services.yml
Be careful because examples are creating parameters and services together into config.yml, you have to adapt it to your case (services.yml and parameters.yml separated, I would recommend your way)
You have the documentation with examples here
https://symfony.com/doc/current/book/service_container.html
I obtained this error when I want to create a config paramter to a bundle.
Symfony\Component\DependencyInjection\Exception\InvalidArgumentException]
There is no extension able to load the configuration for
"mrc_morales_tyre"
This is the code:
app/config/config.yml
mrc_morales_tyre:
db_driver: orm
Configuration.php
namespace MrcMorales\TyreBundle\DependencyInjection;
use Symfony\Component\Config\Definition\Builder\TreeBuilder;
use Symfony\Component\Config\Definition\ConfigurationInterface;
/**
* This is the class that validates and merges configuration from your app/config files
*
* To learn more see {#link http://symfony.com/doc/current/cookbook/bundles/extension.html#cookbook-bundles-extension-config-class}
*/
class Configuration implements ConfigurationInterface
{
/**
* {#inheritdoc}
*/
public function getConfigTreeBuilder()
{
$treeBuilder = new TreeBuilder();
$rootNode = $treeBuilder->root('mrc_morales_tyre');
$supportedDrivers = array('orm');
$rootNode
->children()
->scalarNode('db_driver')
->validate()
->ifNotInArray($supportedDrivers)
->thenInvalid('The driver %s is not supported. Please choose one of '.json_encode($supportedDrivers))
->end()
->end()
->end();
return $treeBuilder;
}
}
TyreExtension.php
namespace MrcMorales\TyreBundle\DependencyInjection;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\Config\FileLocator;
use Symfony\Component\HttpKernel\DependencyInjection\Extension;
use Symfony\Component\DependencyInjection\Loader;
use Symfony\Component\Config\Definition\Processor;
/**
* This is the class that loads and manages your bundle configuration
*
* To learn more see {#link http://symfony.com/doc/current/cookbook/bundles/extension.html}
*/
class TyreExtension extends Extension
{
/**
* {#inheritdoc}
*/
public function load(array $configs, ContainerBuilder $container)
{
$configuration = new Configuration();
$config = $this->processConfiguration($configuration, $configs);
$loader = new Loader\YamlFileLoader($container, new FileLocator(__DIR__.'/../Resources/config'));
}
}
TyreBundle.php
namespace MrcMorales\TyreBundle;
use Symfony\Component\HttpKernel\Bundle\Bundle;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use MrcMorales\TyreBundle\DependencyInjection\Compiler\ValidationPass;
class TyreBundle extends Bundle
{
public function build(ContainerBuilder $container)
{
parent::build($container);
$container->addCompilerPass(new ValidationPass());
}
}
ValidationPass is to load validation.yml in not usual folder and it works.
Thanks
Resolved:
Symfony by default expects than config var name is the name of extension, in this case tyre.
Then I need to change the Extension name to MrcMoralesTyreExtension but then we need override the method getContainerExtension() in:
TyreBundle.php
public function getContainerExtension()
{
return new MrcMoralesTyreExtension();
}
I have defined in app/config.yml sample:
module_my_module
key: value1
key2: value2
And a bundle with DependecyInjection Configuration.php:
<?php
namespace Modules\Bundle\MyModuleBundle\DependencyInjection;
use Symfony\Component\Config\Definition\Builder\TreeBuilder;
use Symfony\Component\Config\Definition\ConfigurationInterface;
class Configuration implements ConfigurationInterface
{
public function getConfigTreeBuilder()
{
$treeBuilder = new TreeBuilder();
$rootNode = $treeBuilder->root('module_configuration');
$rootNode->children()
->scalarNode('key')->defaultValue('')->end()
->scalarNode('key2')->defaultValue('')->end()
->end()
;
return $treeBuilder;
}
}
A problem is how to call config.Bundle/Command class:
namespace Modules\Bundle\MyBundle\Command;
use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;
//use \Modules\Bundle\NodeSocketBundle\DependencyInjection as DependencyInjection;
class MyCommand extends ContainerAwareCommand
{
protected function execute(InputInterface $input, OutputInterface $output)
{
$this->getParams("key.val"); // GEt a key from config.yml
}
}
Config.yml:
module_configuration
key: val
You must inject your params in the Container in the Extension Bundle Class definition.
In your case you must have something similar to:
<?php
namespace Modules\Bundle\MyModuleBundle\DependencyInjection;
class MyModuleExtension extends Extension
{
/**
* {#inheritDoc}
*/
public function load(array $configs, ContainerBuilder $container)
{
$configuration = new Configuration();
$config = $this->processConfiguration($configuration, $configs);
$loader = new Loader\YamlFileLoader($container, new FileLocator(__DIR__.'/../Resources/config'));
$loader->load('...');
$container->setParameter('my_first_key', $config['key']);
$container->setParameter('my_second_key', $config['key2']);
}
}
Then you can access to the value, in your Commandclass as:
class MyCommand extends ContainerAwareCommand
{
protected function execute(InputInterface $input, OutputInterface $output)
{
$this->getContainer()->getParameter("my_first_key");
}
}
Hope this help