Depedendency Injection on base Controller / Service - symfony

i'm very new in Symfony. I began my first project in symfony 2.3 LTS. At this time i have created many controllers in different bundles. My question relates to a correct procedure. I use in my controllers different services, like 'translator', 'form factory', 'session', 'router', 'entitymanager', 'templating'.
I create in my "service.yml" a base service
myapp.controller.base:
class: "%myapp.controller.base.class%"
arguments: ["#doctrine.orm.entity_manager", , "#templating", "#session", "#translator", "#router", "#form.factory"]
and inject the base service in my controller, which is also a service.
myapp.controller.locales:
class: "%myapp.controller.locales.class%"
arguments: ["#myapp.controller.base"]
My base service look like this.
namespace MyApp\<BundleName>\Controller;
use Symfony\Bundle\FrameworkBundle\Templating\EngineInterface;
use Symfony\Component\HttpFoundation\Session\Session;
class BaseController
{
protected $formErrorService;
protected $entityManager;
protected $templating;
protected $session;
protected $translator;
protected $router;
protected $formFactory;
public function __construct($entityManager, EngineInterface $templating, Session $session, $translator, $router, $formFactory)
{
$this->setEntityManager($entityManager);
$this->setTemplating($templating);
$this->setSession($session);
$this->setTranslator($translator);
$this->setRouter($router);
$this->setFormFactory($formFactory);
}
public function setEntityManager($em)
{
$this->entityManager = $em;
}
public function getEntityManager()
{
return $this->entityManager;
}
public function setTemplating($templating)
{
$this->templating = $templating;
}
public function getTemplating()
{
return $this->templating;
}
public function setSession($session)
{
$this->session = $session;
}
public function getSession()
{
return $this->session;
}
public function setTranslator($translator)
{
$this->translator = $translator;
}
public function getTranslator()
{
return $this->translator;
}
public function setRouter($router)
{
$this->router = $router;
}
public function getRouter()
{
return $this->router;
}
public function setFormFactory($formFactory)
{
$this->formFactory = $formFactory;
}
public function getFormFactory()
{
return $this->formFactory;
}
}
And i call in my locales controller look like this
namespace MyApp\<Bundlename>\Controller;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\HttpFoundation\Session\Session;
class LocalesController extends BaseController
{
protected $baseService;
public function __construct($baseService)
{
$this->baseService = $baseService;
}
public function indexAction(Request $request)
{
$locales=$this->baseService->getEntityManager()->getRepository('<reponame>')->findAll();
return $this->baseService->getTemplating()->renderResponse('<templatename>', array('locales' => $locales));
}
Is this a correct procedure? Should i use this procedure in ervery controller which i need this services?
I hope i get useful hints, many thanks

It's an incorrect way to inject one controller to another. You should use parent for your child controllers.
Example:
myapp.controller.base:
class: "%myapp.controller.base.class%"
arguments: ["#doctrine.orm.entity_manager", , "#templating", "#session", "#translator", "#router", "#form.factory"]
myapp.controller.locales:
class: "%myapp.controller.locales.class%"
parent: myapp.controller.base
More docs about parent services and overriding parent dependecies: http://symfony.com/doc/current/components/dependency_injection/parentservices.html

Related

HWIOAuthBundle add token attribute after success auth

I use HWIOAuthBundle for Symfony.
HWI\Bundle\OAuthBundle\Security\Core\Authentication\Provider\OAuthProvider.php has function "authenticate" where create and return OAuthToken.
public function authenticate(TokenInterface $token){
...
$token = new OAuthToken($oldToken->getRawToken(), $user->getRoles());
$token->setResourceOwnerName($resourceOwner->getName());
$token->setUser($user);
$token->setAuthenticated(true);
$token->setRefreshToken($oldToken->getRefreshToken());
$token->setCreatedAt($oldToken->getCreatedAt());
return $token;
}
I need set some attribute to this OAuthToken like :
$token->setAttribute('name' => 'value');
How can i do this ?
I use service with only one function "loadUserByOAuthUserResponse" which return user object :
class PartnerEntityUserProvider implements OAuthAwareUserProviderInterface{
...
public function loadUserByOAuthUserResponse(UserResponseInterface $response){
...
return $user;
}
...
}
Edited
I found solution with using service on security.interactive_login event :
config/services.yaml
services:
App\EventListener\InteractiveLoginListener:
tags:
- { name: kernel.event_listener, event: security.interactive_login }
src/EventListener/InteractiveLoginListener.php
<?php
namespace App\EventListener;
use App\Entity\Perimetre;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Component\Security\Core\Security;
use Symfony\Component\Security\Http\Event\InteractiveLoginEvent;
class InteractiveLoginListener
{
private $security;
private $entityManager;
public function __construct(Security $security, EntityManagerInterface $entityManager)
{
$this->security = $security;
$this->entityManager = $entityManager;
}
public function onSecurityInteractiveLogin(InteractiveLoginEvent $event)
{
$this->security->getToken()->setAttribute('perimetre', $this->entityManager->getRepository(Perimetre::class)->findOneBy(['id' => $this->security->getToken()->getUser()->getFiliale()->getPerimetre()->getId()]));
}
}

Symfony Worker - Consume Messages - EntityManager

I use Symfony 4 with messenger and I use a worker who consumes my messages as a long-running process.
I have a bug with doctrine if I delete my post and recreate a new one and I dispatch my message. $post have the old data and not the new one.
I have tried a lot of things and nothing work, it works when I restart my worker.
class ChannelMessageHandler implements MessageHandlerInterface
{
private $channel;
private $bus;
public function __construct(ChannelService $channel, MessageBusInterface $commandBus)
{
$this->channel = $channel;
$this->bus = $commandBus;
}
public function __invoke(ChannelMessage $channelMessage)
{
$error = $this->channel->handleChannel($channelMessage->getUser(), $channelMessage->getService());
if ($error) {
throw new Exception($error[0]);
}
$this->bus->dispatch(new FeedMessage($channelMessage->getUser(), $channelMessage->getService()));
}
}
}
My MessageHandler call a service :
class ChannelService implements ContainerAwareInterface
{
use ContainerTrait;
protected $em;
protected $logger;
public function __construct(EntityManagerInterface $entityManager, LoggerInterface $logger)
{
$this->em = $entityManager;
$this->logger = $logger;
}
public function handleChannel($userId, $serviceName)
{
$user = $this->em->getRepository('App:User\Authentication\User')->findOneById($userId);
$post = $user->getPost();
return $this->getUserAnalyticBy($post, $serviceName);
}
thanks a lot

Symfony 3.4: Access doctrine within an EventListener

I am trying to access to doctrine within an EventListener which tests a database table, if it is invalid then the user will be redirected to a config page where he will fix the issues!
The test will be executed before every call to a controller, so I will use the Event kernel.controller:
[EDITED: SOLVED, TESTING AND REDIRECTING VERY FINE]
<?php
namespace AdminBundle\EventListener;
use Symfony\Bundle\FrameworkBundle\Routing\Router;
use Doctrine\ORM\EntityManager;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\HttpFoundation\RequestStack;
use Symfony\Component\HttpFoundation\Session\Session;
class BuildReferencesEventListener
{
/**
* #var EntityManager
*/
protected $em;
/**
* #var Router
*/
protected $router;
protected $references = [
1 => 'oxygen',
2 => 'pH',
3 => 'pCO2'
];
protected $redirect_route = 'references';
protected $requestStack;
protected $session;
public function __construct(EntityManager $entityManager, Router $router, RequestStack $requestStack, Session $session)
{
$this->em = $entityManager;
$this->router = $router;
$this->requestStack = $requestStack;
$this->session = $session;
}
public function onKernelController()
{
$em = $this->em;
$savedReferences = $em->getRepository('AdminBundle:ParamReference')->findAll();
$references = $this->references;
if (count($savedReferences) <= 0){
$this->redirect();
}
for ($i =0; $i<count($savedReferences); $i++){
if ($savedReferences[$i] !== $references[$i]) {
$this->redirect();
}
}
}
public function redirect()
{
$request = $this->requestStack->getCurrentRequest();
$route = $request->get('_route');
$this->session->getFlashBag()->add('warning', 'You need to setup the references for the parameters before starting the work');
if ($route != 'references'){
$url = $this->router->generate('references');
$redirect = new RedirectResponse($url);
$redirect->send();
}
}
}
Here is services.yml
build.references:
class: AdminBundle\EventListener\BuildReferencesEventListener
arguments: ['#doctrine.orm.entity_manager', '#router', '#request_stack', '#session']
tags:
- { name: kernel.event_listener, event: kernel.controller, method: onKernelController }
Add argument inside your service.yml event listner service section
Your.service:
Class: AdminBundle\EventListener\BuildReferencesEventListener
arguments: ['#doctrine.orm.entity_manager']
tags:
- { name: kernel.event_listener, event: kernel.controller }
And add this __cunstruct code inside your listner:
namespace AdminBundle\EventListener;
use Symfony\Component\HttpKernel\Event\FilterControllerEvent;
use Doctrine\ORM\EntityManager;
class BuildReferencesEventListener
{
protected $em;
function __construct(EntityManager $em)
{
$this->em = $em;
}
public function onKernelController(FilterControllerEvent $event)
{
//TO DO call doctrine
// $em = Enity Manager...
// $em->getRepository...
// I hope its clear enough what im trying to do
}
In Symfony 3.4 and above, autowiring is enabled by default, so you just have to add a type-hinted argument to your service's constructor method and a private property to assign the argument's value to.
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Component\HttpKernel\Event\FilterControllerEvent;
class BuildReferencesEventListener
{
private $em;
public __construct(EntityManagerInterface $em)
{
$this->em = $em;
}
public function onKernelController(FilterControllerEvent $event)
{
$em = this->em;
// [...]
}
}
If you need to
declare arguments explicitly, you could do this in your service configuration:
# config/services.yaml
services:
# ... same code as before
# explicitly configure the service
AdminBundle\EventListener\BuildReferencesEventListener:
arguments:
$em: '#doctrine.orm.entity_manager_interface'
This could be useful to pass parameters that the service container doesn't know.
To learn more about the service container, https://symfony.com/doc/3.4/service_container.html#injecting-services-config-into-a-service.

Symfony 3.1 Listener with access to database

I have a config table with a 'web_enabled' key, when true I want to show the route requested but when false I want to show a 'Site in maintenance' page. Obviously this check has to be performed before any route action.
I have been reading about Events and Listeners but I don't see how to implement the access to the doctrine and template.
Thanks for the help.
This is the solution I implemented finally, differs from the proposed by Alsatian because I don't use a parameter in the service.yml. Is just a question of taste, nothing else.
in app/config/services.yml
services:
app.request_listener:
class: AppBundle\EventListener\RequestListener
arguments: ["#doctrine.orm.entity_manager","#templating"]
tags:
- { name: kernel.event_listener, event: kernel.request, method: onKernelController }
in src/AppBundle/EventListener/RequestListener.php
namespace AppBundle\EventListener;
use \Symfony\Component\HttpKernel\Event\GetResponseEvent;
use \Symfony\Component\HttpFoundation\Response;
class RequestListener
{
private $em;
private $templating;
public function __construct($em, $templating)
{
$this->em = $em;
$this->templating = $templating;
}
public function onKernelController(GetResponseEvent $event)
{
if ( !$this->configKey = $this->em->getRepository('AppBundle:Config')->getconfig('web_enabled') )
$event->setResponse($this->templating->renderResponse('default/construction.html.twig'));
}
}
and in src/AppBundle/Repository/ConfigRepository.php explaining the getconfig method:
namespace AppBundle\Repository;
class ConfigRepository extends \Doctrine\ORM\EntityRepository
{
public function getconfig( $config_name )
{
$config = $this->getEntityManager()
->createQuery('SELECT p.config_value FROM AppBundle:Config p WHERE p.config_name = :config_name')
->setParameter('config_name', $config_name)
->getResult();
if (sizeof($config)){
return $config[0]['config_value'];
}else{
return false;
}
}
}
Hope this helps.
You have just to inject both EntityManager and Templating in your listener :
Definition as service :
# src/AppBundle/Ressources/config/services.yml
services:
app.request_listener:
class: AppBundle\EventListener\RequestListener
arguments: ["%web_enabled%","#doctrine.orm.entity_manager","#templating"]
tags:
- { name: kernel.event_listener, event: kernel.request, method: onKernelRequest }
Listener :
namespace AppBundle\EventListener;
use \Symfony\Component\HttpKernel\Event\GetResponseEvent;
use \Symfony\Component\HttpFoundation\Response;
class RequestListener
{
private $configKey;
private $em;
private $templating;
public __construct($configKey, $em, $templating)
{
$this->configKey = $configKey;
$this->em = $em;
$this->templating = $templating;
}
public function onKernelRequest(GetResponseEvent $event)
{
if(!$this->configKey){
$var = $this->em->getRepository('AppBundle:MyEntity')->findOne(1);
$event->setResponse($this->templating->renderResponse('my_template.html.twig',array('var'=>$var));
}
}
}

how to redirect after login success by using EventListener

I have a problem in the code of event listener because i need to make a EventListener when i login as type 1 ==>redirect to:
return $this->redirect($this->generateUrl('demands_patient'));
else redirect to another path .And this is the code of loginlistener
use Doctrine\ORM\Event\LifecycleEventArgs;
use Register\UserBundle\Entity\User;
use FOS\UserBundle\FOSUserEvents;
use FOS\UserBundle\Event\FormEvent;
use FOS\UserBundle\Event\GetResponseUserEvent;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
use Symfony\Component\Security\Http\Event\InteractiveLoginEvent;
use Symfony\Component\Security\Core\AuthenticationEvents;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\Security\Http\Event\InteractiveLoginEvent;
class LoginListener implements EventSubscriberInterface
{
private $router;
public function __construct(UrlGeneratorInterface $router)
{
$this->router = $router;
}
public static function onSecurityInteractiveLogin()
{
return array(
FOSUserEvents::AUTHENTICATION_SUCCESS => 'redirectlogin',
);
}
public function redirectlogin(InteractiveLoginEvent $event)
{
$user = $event->getUser();
if($user->getType()=="1")
{
return $this->redirect($this->generateUrl('demands_patient'));
}
else
{
return $this->redirect($this->generateUrl('demands'));
}
}
}
and i add configuration in services.yml:
register_user.eventlistener.loginlistener:
class: Register\UserBundle\EventListener\LoginListener
arguments: [#router]
tags:
- { name: kernel.onSecurityInteractiveLogin }
Just today I faced a similar problem.
My solution was to use a Handler instead of a Listener, as explained in http://www.reecefowell.com/2011/10/26/redirecting-on-loginlogout-in-symfony2-using-loginhandlers/
What I did is:
In my bundle's `services.yml':
parameters:
cambra_comunitats.handler.user_login_redirect_handler.class: Cambra\ComunitatsBundle\Handler\UserLoginRedirectHandler
services:
cambra_comunitats.handler.user_login_redirect_handler:
class: "%cambra_comunitats.handler.user_login_redirect_handler.class%"
arguments: [#router, #security.context]
tags:
- { name: 'monolog.logger', channel: 'security' }
Then, I created the Cambra\ComunitatsBundle\Handler\UserLoginRedirectHandler class:
class UserLoginRedirectHandler implements \Symfony\Component\Security\Http\Authentication\AuthenticationSuccessHandlerInterface
{
protected $router;
protected $security;
public function __construct(\Symfony\Component\Routing\Router $router, \Symfony\Component\Security\Core\SecurityContext $security)
{
$this->router = $router;
$this->security = $security;
}
public function onAuthenticationSuccess(\Symfony\Component\HttpFoundation\Request $request, \Symfony\Component\Security\Core\Authentication\Token\TokenInterface $token)
{
if ($this->security->isGranted('ROLE_ADMIN') && $this->security->getToken()->getUser()->isMustRedirect())
{
$response = new \Symfony\Component\HttpFoundation\RedirectResponse($this->router->generate('new_destination'));
} else
{
$referer_url = $this->router->generate('index_page');
$response = new \Symfony\Component\HttpFoundation\RedirectResponse($referer_url);
}
return $response;
}
}
And, finally, I enabled it on security.yml, inside of firewalls.main.form_login:
//.....
firewalls:
//...
main:
//...
form_login:
//...
success_handler: cambra_comunitats.handler.user_login_redirect_handler

Resources