overriding registration FOSUserBundle Symfony2 - symfony

I am trying to override Registration Form in FOSUserBundle but i get this error:
i have followed this tutorial in the official documentation:
Link
Could not load type "uae_user_registration"
My files are:
services.yml
# src/Uae/UserBundle/Resources/config/services.yml
services:
uae_user.registration.form.type:
class: Uae\UserBundle\Form\Type\RegistrationFormType
arguments: [%fos_user.model.user.class%]
tags:
- { name: form.type, alias: uae_user_registration }
config.yml:
app/config/config.yml
fos_user:
db_driver: orm
firewall_name: main
user_class: Uae\UserBundle\Entity\User
registration:
form:
type: uae_user_registration
RegistrationFormType:
<?php
#src/Uae/UserBundle/Form/Type/RegistrationType.php
namespace Uae\UserBundle\Form\Type;
use Symfony\Component\Form\FormBuilderInterface;
use FOS\UserBundle\Form\Type\RegistrationFormType as BaseType;
class RegistrationFormType extends BaseType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
parent::buildForm($builder, $options);
// add your custom field
$builder->add('nom');
$builder->add('prenom');
}
public function getName()
{
return 'uae_user_registration';
}
}

i solved my problem:
i just imported the new service that i created in the config file
app\config\config.yml
imports:
- { resource: #UaeUserBundle/Resources/config/services.yml }

The reason you're getting the error is because you do not have a DependencyInjection for your specific bundle. The program doesn't know where to look for your services.yml file.
You need an UaeUserExtension.php and Configuration.php inside your DependencyInjection folder under your User Bundle.
The easy solution to this is to generate the bundle via app/console generate:bundle. This way, it'll create your DependencyInjection for you automatically.
The manual solution would be to create a DependencyInjection folder inside your Uae/UserBundle. Inside DependencyInjection, create a file called Configuration.php and place the contents below:
<?php
namespace Uae\UserBundle\DependencyInjection;
use Symfony\Component\Config\Definition\Builder\TreeBuilder;
use Symfony\Component\Config\Definition\ConfigurationInterface;
class Configuration implements ConfigurationInterface
{
/**
* {#inheritDoc}
*/
public function getConfigTreeBuilder()
{
$treeBuilder = new TreeBuilder();
$rootNode = $treeBuilder->root('uae_user');
return $treeBuilder;
}
}
And create a file called UaeUserExtension.php inside the same directory and place these contents inside:
<?php
namespace Uae\UserBundle\DependencyInjection;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\Config\FileLocator;
use Symfony\Component\HttpKernel\DependencyInjection\Extension;
use Symfony\Component\DependencyInjection\Loader;
class EnergyUserExtension 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');
}
}

Related

symfony6 - defining and using services.yaml in third part bundle

I'm making simple "MyCoreBundle" (MystertyCoreBundle) using symfony6.1 how to make bundle's doc.
I defined my bundle class vendor/mysterty/core-bundle/CoreBundle.class
<?php
namespace Mysterty\CoreBundle;
use Symfony\Component\HttpKernel\Bundle\AbstractBundle;
class MystertyCoreBundle extends AbstractBundle
{
}
I defined some parameters and configuration in vendor/mysterty/core-bundle/config/services.yaml as defaults :
services:
Mysterty\CoreBundle\Controller\CoreController:
public: true
calls:
- method: setContainer
arguments: ["#service_container"]
parameters:
app.admin_email: "mymailATserver.com"
Then I made simple controller in vendor/mysterty/core-bundle/src/Controller/CoreController.php:
<?php
namespace Mysterty\CoreBundle\Controller;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Contracts\Translation\TranslatorInterface;
class CoreController extends AbstractController
{
#[Route('/', name: 'mty_default')]
public function indexNoLocale(): Response
{
$lang = substr($_SERVER['HTTP_ACCEPT_LANGUAGE'], 0, 2);
$supportedLangs = explode('|', $this->getParameter('app.supported_locales'));
$lang = in_array($lang, $supportedLangs) ? $lang : $supportedLangs[0];
return $this->redirectToRoute('mty_home', ['_locale' => $lang]);
}
Finally, i added the bundle's routes to \config\routes.yaml
mysterty_core:
resource: "../vendor/mysterty/core-bundle/src/Controller/CoreController.php"
type: annotation
prefix: /
Here is the error i have on http://127.0.0.1:8000/ :
"Mysterty\CoreBundle\Controller\CoreController" has no container set, did you forget to define it as a service subscriber?
I try to make a shared bundle with default actions and components for all my symfony projects.
Solution (thx to helpers)
define loadExtension function in MyOwnBundle.php :
<?php
namespace MyOwn\MyOwnBundle;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator;
use Symfony\Component\HttpKernel\Bundle\AbstractBundle;
class MyOwnBundle extends AbstractBundle
{
public function loadExtension(array $config, ContainerConfigurator $container, ContainerBuilder $builder): void
{
// load an XML, PHP or Yaml file
$container->import('../config/services.yaml');
}
}
It looks like Symfiony could not autoconfigure your controller. Try adding the #[AsController] attribute to your controller classes or add autoconfigure: true to your controller service definition in your services.yaml

Error in Loading Service Configuration

I have a CampusCalendarBundle extension. After I added the DependencyInjection folder to load config files. I have this error. I don't think system_configuration.yml is been loaded.
[RuntimeException]
[Symfony\Component\Config\Definition\Exception\InvalidConfigurationException]
The system configuration variable "oro_calendar.calendar_colors" is not
defined. Please make sure that it is either added to bundle
configuration settings or marked as "ui_only" in config.
<?php
namespace CampusCRM\CampusCalendarBundle\DependencyInjection;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\Config\FileLocator;
use Symfony\Component\HttpKernel\DependencyInjection\Extension;
use Symfony\Component\DependencyInjection\Loader;
class CampusCalendarExtension extends Extension
{
/**
* {#inheritDoc}
*/
public function load(array $configs, ContainerBuilder $container)
{
$configuration = new Configuration();
$config = $this->processConfiguration($configuration, $configs);
$container->setParameter(
'oro_calendar.enabled_system_calendar',
$config['enabled_system_calendar']
);
$container->prependExtensionConfig($this->getAlias(), array_intersect_key($config, array_flip(['settings'])));
$loader = new Loader\YamlFileLoader($container, new FileLocator(__DIR__.'/../Resources/config'));
$loader->load('services.yml');
$loader->load('form.yml');
}
}
Here is extension Bundle.
<?php
// src/CampusCRM/CampusCalendarBundle/CampusCalendarBundle.php
namespace CampusCRM\CampusCalendarBundle;
use Symfony\Component\HttpKernel\Bundle\Bundle;
class CampusCalendarBundle extends Bundle
{
public function getParent()
{
return 'OroCalendarBundle';
}
}
I have discovered a solution! I probably accidentally deleted some code in OroCalendarBundle. Everything works after I replaced OroCalendarBundle with a refresh install.
Thank you everyone for your help~

Symfony - There is no extension able to load the configuration - custom config

Helo,
i am trying to load custom config into my AppBundle. Unluckily I am getting:
[Symfony\Component\DependencyInjection\Exception\InvalidArgumentException]
There is no extension able to load the configuration for "app" (in
/var/www
/dev.investmentopportunities.pl/src/AppBundle/DependencyInjection/../Resour
ces/config/general.yml). Looked for namespace "app", found none
There are several similar topics relating to this error. I have looked at them but can't find any solution which would resolve this issue.
My configuration file looks like this:
<?php
namespace AppBundle\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('app');
$rootNode
->children()
->arrayNode('tags')
->prototype('array')
->children()
->scalarNode('name')->isRequired()->end()
->scalarNode('role')->isRequired()->end()
->scalarNode('priority')->isRequired()->end()
->scalarNode('label')->isRequired()->end()
->end()
->end();
return $treeBuilder;
}
}
and general.yml:
app:
tags:
Accepted:
name: "Accepted"
role: "ROLE_ACCEPTTAG"
priority: "3"
label: "label label-info"
Booked:
name: "Booked"
role: "ROLE_ACCOUNTANT"
priority: "3"
label: "label label-info"
Finalized:
name: "Booked"
role: "ROLE_ACCEPTDOC"
priority: "1"
label: "label label-success"
AppExtension.php:
<?php
namespace AppBundle\DependencyInjection;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\Config\FileLocator;
use Symfony\Component\DependencyInjection\Extension\PrependExtensionInterface;
use Symfony\Component\HttpKernel\DependencyInjection\Extension;
use Symfony\Component\DependencyInjection\Loader;
use Symfony\Component\Validator\Tests\Fixtures\Entity;
/**
* This is the class that loads and manages your bundle configuration
*/
class AppExtension extends Extension
{
/**
* {#inheritdoc}
*/
public function load(array $configs, ContainerBuilder $container)
{
$configuration = new Configuration();
$config = $this->processConfiguration($configuration, $configs);
$container->setParameter('app', $config['app']);
$loader = new Loader\YamlFileLoader($container, new FileLocator(__DIR__.'/../Resources/config'));
$loader->load('general.yml'); # another file of yours
}
}
TL;DR: You should not load your general.yml file in the extension.
Defining a configuration for a bundle is about how the bundle will handle configuration, understand the configuration coming from config.yml.
So you should import your general.yml file in the config.yml and it should work.
Note: Loaders are from Symfony\Component\DependencyInjection\Loader namespace, they are for dependency injection, to allow a bundle to define services, mostly used by third-party bundles.

Dependency Injenction in config.yml

I have two services and I want pass my parameter from config.yml
my config.yml
parameters:
MyService.class: Acme\UserBundle\Services\sendEmail
MyService.arguments: #mailer
NewUserListener.class: Acme\UserBundle\Event\NewUserListener
NewUserListener.arguments: #MyService
my service.yml inside bundle
services:
MyService:
class: %MyService.class%
arguments: [%MyService.arguments%]
NewUserListener:
class: %NewUserListener.class%
arguments: [%NewUserListener.arguments%]
tags:
- { name: kernel.event_listener, event: new.user, method: sendEmailToUsers }
I got an error
You cannot dump a container with parameters that contain references to
other services
My Questions are:
How can I inject my arguments from config.yml?
Where can i Find the list of "global service" like #mailer ? i don't find in doc
You can't reference a service in a parameter. You should replace %MyService.arguments% with #mailer.
To find all available services, run php app/console container:debug
This a bit more complicated!
First, you have to declare your default services like that (I changed all the names in order to be compliant with the Symfony2's conventions):
# resources/config/services.yml
services:
my_own.service.default.class: Acme\UserBundle\Services\sendEmail
my_own.user_listener.default.class: Acme\UserBundle\Event\NewUserListener
services:
my_own.service.default:
class: %my_own.service.default.class%
arguments: [#mailer]
my_own.user_listener:
class: %my_own.user_listener.class%
arguments: [#my_own.service]
tags:
- { name: kernel.event_listener, event: new.user, method: sendEmailToUsers }
We will define some configuration for your bundle in order to allow to change the used services:
namespace My\OwnBundle\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('my_own');
// Here you should define the parameters that are allowed to
// configure your bundle. See the documentation linked above for
// more information on that topic.
$rootNode
->children()
->scalarNode('service')->defaultValue('my_own.service.default')->end()
->scalarNode('user_listener')->defaultValue('my_own.user_listener.default')->end()
->end();
return $treeBuilder;
}
}
Note that, by default, we use our default services defined above in our bundle.
You now can use the following to change your services (in your app/config.yml) for instance:
# app/config.yml
my_own:
service: my_other.service
user_listener: my_other.user_listener
Of course, you can define the services my_other.service and my_other.user_listener as you want in your bundle or in another bundle.
Now we have to tell how to use this configuration to take the wanted services:
namespace My\OwnBundle\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 MyOwnExtension 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');
$container->setAlias('my_own.service', $config['service']);
$container->setAlias('my_own.user_listener', $config['user_listener']);
}
}
Finally, in the rest of your code you have to use the aliased services my_own.service and my_own.user_listener in your code:
// In one of your controller:
$this->container->get('my_own.service');
/* or directly */ $this->get('my_own.service'); // if your controller is a child of the framework bundle class `Controller`.

How can I listen to console events in symfony?

I'm trying to hook into symfonys console events with the symfony standard edition (2.3), but it just won't work.
I created a listener according to their example and follow the guides on event registration:
namespace Acme\DemoBundle\EventListener;
use Symfony\Component\Console\Event\ConsoleCommandEvent;
use Symfony\Component\Console\ConsoleEvents;
class AcmeCommandListener
{
public function onConsoleCommand(ConsoleCommandEvent $event) {
// get the output instance
$output = $event->getOutput();
// get the command to be executed
$command = $event->getCommand();
// write something about the command
$output->writeln(sprintf('Before running command <info>%s</info>', $command->getName()));
}
}
and someone on the mailing list told me to register it as event in the service container. So I did this:
services:
kernel.listener.command_dispatch:
class: Acme\DemoBundle\EventListener\AcmeCommandListener
tags:
- { name: kernel.event_listener, event: console.command }
But obviously the tagging is not correct and I can't find the correct names for that. How would I do that?
Platform\EventListener\Console\InitListener:
tags:
- { name: kernel.event_listener, event: console.command, priority: 1024 }
<?php
class CustomListener
{
public function onConsoleCommand(ConsoleCommandEvent $event): void
{
//do somehting
}
}
?>
So, I finally got it. The above code in the original post ist fully working, but I defined my services.yml within my bundle not in the application config app/config.yml. This means, the configuration was never loaded. I had to import the configuration via container extensions:
# Acme/DemoBundle/DependencyInjection/AcmeDemoExtension.php
namespace Acme\DemoBundle\DependencyInjection;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\Config\FileLocator;
use Symfony\Component\HttpKernel\DependencyInjection\Extension;
use Symfony\Component\DependencyInjection\Loader;
class AcmeDemoExtension extends Extension
{
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');
}
}
and
# Acme/DemoBundle/DependencyInjection/Configuration.php
namespace Acme\DemoBundle\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('headwork_legacy');
return $treeBuilder;
}
}
Though I guess you can even leave out the $configuration = new Configuration(); part and the Configuration class.
Your tags event listener name must be a console.event_listener. It's has helped me to resolve this issue.
services:
kernel.listener.command_dispatch:
class: Acme\DemoBundle\EventListener\AcmeCommandListener
tags:
- { name: console.event_listener, event: console.command }

Resources