Inject Doctrine Repository into controller - symfony

I had the following controller:
<?php
namespace AppBundle\Controller;
use AppBundle\Controller\BaseController;
use Symfony\Component\HttpFoundation\Request;
class UserController extends BaseController
{
public function allAction(Request $request)
{
$users = $this->getDoctrine()
->getRepository('AppBundle:User')
->findAll();
return $this->respond(['users' => $users], 200);
}
}
And I would like to inject the repository into the controller (for testing purposes)
Controller:
<?php
namespace AppBundle\Controller;
use AppBundle\Controller\BaseController;
use Doctrine\ORM\EntityRepository;
use Symfony\Component\HttpFoundation\Request;
class UserController extends BaseController
{
private $userrepository;
public function __construct(EntityRepository $userrepository)
{
$this->userrepository = $userrepository;
}
public function allAction(Request $request)
{
$users = $this->userrepository->findAll();
return $this->respond(['users' => $users], 200);
}
}
services.yml
services:
userrepository:
class: Doctrine\ORM\EntityRepository
factory_service: doctrine.orm.entity_manager
factory_method: getRepository
arguments:
- AppBundle\Entity\User
usercontroller:
class: AppBundle\Controller\UserController
arguments:
- "#userrepository"
routing.yml
api_users_all:
path: /api/users.{_format}
defaults: { _controller: usercontroller:allAction, _format: json }
requirements:
_method: GET
I keep facing the following error:
PHP Fatal error: Call to a member function get() on a non-object in /Applications/MAMP/htdocs/api/vendor/symfony/symfony/src/Symfony/Bundle/FrameworkBundle/Controller/Controller.php on line 350
line 350
return $this->container->get($id);
So my guess is that the controller is no longer 'ContainerAware' or something like that.
But I can't seem to figure this out.
I am working with symfony 2.7

After you configure the router to call controller via service, controller is not created directly but requested from the Dependency Injection container. And yes, there is no longer container because you inject only one parameter to it: user repository.
If you want whole container (which is not recommended) and you extending Symfony Controller in your BaseController set it in your service:
services:
usercontroller:
class: AppBundle\Controller\UserController
calls:
- [setContainer, ["#service_container"]]
arguments:
- "#userrepository"
If you not extending Symfony Controller, inject container as argument:
services:
usercontroller:
class: AppBundle\Controller\UserController
arguments:
- "#userrepository"
- "#service_container"
Controller:
class UserController extends BaseController
{
private $userrepository;
private $container;
public function __construct(EntityRepository $userrepository, $container)
{
$this->userrepository = $userrepository;
$this->container = $container;
}
It's not recommended, inject only what you want eg. if you use in your controller only repository and mailer inject only them not the whole container.

Related

Cannot autowire service in Symfony 3.4 and FosUserBundle

I try to override REGISTRATION_SUCCESS in FosUserBundle to redirect the admin on user's list after register a new user.
So I have created a new event subscriber :
<?php
namespace AppBundle\EventListener;
use FOS\UserBundle\Event\FormEvent;
use FOS\UserBundle\FOSUserEvents;
use FOS\UserBundle\Mailer\MailerInterface;
use FOS\UserBundle\Util\TokenGeneratorInterface;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
class RedirectAfterRegistrationSubscriber implements EventSubscriberInterface
{
private $mailer;
private $tokenGenerator;
private $router;
public function __construct(MailerInterface $mailer, TokenGeneratorInterface $tokenGenerator, UrlGeneratorInterface $router)
{
$this->mailer = $mailer;
$this->tokenGenerator = $tokenGenerator;
$this->router = $router;
}
public function onRegistrationSuccess(FormEvent $event)
{
$user = $event->getForm()->getData();
$user->setEnabled(false);
if (null === $user->getConfirmationToken()) {
$user->setConfirmationToken($this->tokenGenerator->generateToken());
}
$this->mailer->sendConfirmationEmailMessage($user);
$url = $this->router->generate('user_index');
$event->setResponse(new RedirectResponse($url));
}
public static function getSubscribedEvents()
{
return [
FOSUserEvents::REGISTRATION_SUCCESS => 'onRegistrationSuccess'
];
}
}
and the following service :
app.redirect_after_registration_subscriber:
class: AppBundle\EventListener\RedirectAfterRegistrationSubscriber
arguments: ['#fos_user.mailer', '#fos_user.util.token_generator', '#router']
tags:
- { name: kernel.event_subscriber }
I don't understand why this error appears :
Cannot autowire service "AppBundle\EventListener\RedirectAfterRegistrationSubscriber":
argument "$mailer" of method "__construct()" references interface
"FOS\UserBundle\Mailer\MailerInterface" but no such service exists. You should maybe alias
this interface to one of these existing services: "fos_user.mailer.default",
"fos_user.mailer.twig_swift", "fos_user.mailer.noop".
I suppose you are using autodiscovering of services. Something like:
# services.yaml
AppBundle\:
resource: '../src/'
...
So in addition to the #app.redirect_after_registration_subscriber that you define, Symfony defines another service with id #AppBundle\EventListener\RedirectAfterRegistrationSubscriber. Both point to AppBundle\EventListener\RedirectAfterRegistrationSubscriber class. Yet you configured the mailer parameter only for the first one.
The solution:
AppBundle\EventListener\RedirectAfterRegistrationSubscriber:
arguments: ['#fos_user.mailer', '#fos_user.util.token_generator', '#router']
tags:
- { name: kernel.event_subscriber }
With autowiring and autoconfigure you can even sypmlify to:
AppBundle\EventListener\RedirectAfterRegistrationSubscriber:
arguments:
$mailer: '#fos_user.mailer'

how make services in symfony3

i have this controller
namespace InicioBundle\Controller;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Request;
use EntidadesBundle\Entity\Usuarios;
use Symfony\Component\HttpFoundation\Session\Session;
class DefaultController extends Controller
{
private $session;
public function __construct(){
$this->session = new Session();
}
.....
public function ver_rol($rol){
if($this->sacarRol() === $rol){
return true;
}else{
return false;
}
}
}
and in the services.yml , i got this:
parameters:
#parameter_name: value
services:
app.rolSession:
class: InicioBundle\Controller\DefaultController
arguments: ["i dont know how get paramets"]
the problem is that it doesnt work, symfony return an error FileLoaderLoadException, that the services.yml does not caontain valid YAML
There is a space before parameters: in your services.yml file, maybe remove that and your yaml should be valid.
Also if you are passing no arguments to constructor you can just delete arguments: ["null"]
One more thing, IIRC you need to add FQCN as class name, so class: InicioBundle\Controller\Default => class: InicioBundle\Controller\DefaultController
While we are at the subject, you can type hint Request in your action and use it to getSession() or maybe inject #session service to your controller

Fosuserbundle override event listener

I'm trying to override the LastLoginListener to add functionality to it.
I;m trying to do it as described here
It seems
In AppBundle\DependencyInjection\OverrideServiceCompilerPass.php
<?php
namespace AppBundle\DependencyInjection\Compiler;
use AppBundle\EventListener\LastLoginListener;
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
use Symfony\Component\DependencyInjection\ContainerBuilder;
class OverrideServiceCompilerPass implements CompilerPassInterface
{
public function process(ContainerBuilder $container)
{
$definition = $container->getDefinition('"fos_user.security.interactive_login_listener');
$definition->setClass(LastLoginListener::class);
}
services.yml
services:
app.login_listener:
class: AppBundle\EventListener\LastLoginListener
arguments: []
tags:
- { name: kernel.event_subscriber }
The listener itself is copied from the bundle.
The autoloader expected class "AppBundle\DependencyInjection\OverrideServiceCompilerPass" to be defined in file "/vendor/composer/../../src/AppBundle/DependencyInjection/OverrideServiceCompilerPass.php". The file was found but the class was not in it, the class name or namespace probably has a typo.
in DebugClassLoader.php (line 261)
My goal is to add the ip address of the last login with the listener, but I'll need to create another to add a role and a registration date
I'm trying to do it "the right way" instead of doing something hackish
Its much better to use success_handler and failure_handler services.
# app/config/security.yml
firewalls:
main:
...
form_login:
...
success_handler: authentication_success_handler
failure_handler: authentication_failure_handler
Next you need to register your services and add arguments that fit your needs (probably #router and #doctrine.orm.entity_manager)
# app/config/services.yml
authentication_success_handler:
class: AppBundle\Handler\AuthenticationSuccessHandler
arguments: ['#router', '#doctrine.orm.entity_manager']
authentication_failure_handler:
class: AppBundle\Handler\AuthenticationFailureHandler
arguments: ['#router', '#doctrine.orm.entity_manager']
Then you need to create your services
// src/AppBundle/Handler/AuthenticationSuccessHandler.php
<?php
namespace AppBundle\Handler;
use Symfony\Component\Security\Http\Authentication\AuthenticationSuccessHandlerInterface;
use Symfony\Component\Security\Core\Exception\AuthenticationException;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Routing\Router;
use Doctrine\Common\Persistence\ObjectManager;
class AuthenticationSuccessHandler implements AuthenticationSuccessHandlerInterface {
protected $router;
private $em;
public function __construct(Router $router, ObjectManager $em) {
$this->router = $router;
$this->em = $om;
}
public function onAuthenticationSuccess(Request $request, AuthenticationException $exception) {
// your code here - creating new object. redirects etc.
}
}
and
// src/AppBundle/Handler/AuthenticationFailureHandler.php
<?php
namespace AppBundle\Handler;
use Symfony\Component\Security\Http\Authentication\AuthenticationFailureHandlerInterface;
use Symfony\Component\Security\Core\Exception\AuthenticationException;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Routing\Router;
use Doctrine\Common\Persistence\ObjectManager;
class AuthenticationFailureHandler implements AuthenticationFailureHandlerInterface {
protected $router;
private $em;
public function __construct(Router $router, ObjectManager $em) {
$this->router = $router;
$this->em = $om;
}
public function onAuthenticationFailure(Request $request, AuthenticationException $exception) {
// your code here - creating new object. redirects etc.
}
}
If you want to hook into another FOSUserBundle Controller use this

Symfony 3 inject service into controller

I'm using symfony 3.1.7 and I have problems with inject services into a controller, my english is bad I believe that it is better if I show all the code.
The error:
Type error: Argument 1 passed to
...Bundle\Controller\AdminController::__construct() must
implement interface
Symfony\Component\DependencyInjection\ContainerInterface, none given,
called in /......./project/var/cache/dev/classes.php on line 2512
This is my code:
GenericRestController
namespace MyCoreBundle\Controller;
use Symfony\Component\DependencyInjection\ContainerInterface as Container;
use MyCoreBundle\Handler\GenericRestHandlerInterface as Generic;
/** other uses like FOSRestController **/
/**
* Class GenericRestController
*/
abstract class GenericRestController extends FOSRestController
{
protected $handler;
protected $container;
public function __construct(Container $container,Generic $handler)
{
$this->container = $container;
$this->handler = $handler;
}
/** other methods like get, post, put, etc **/
}
AdminController
namespace ...Bundle\Controller;
use MyCoreBundle\Controller\GenericRestController;
class AdminController extends GenericRestController
{
}
RESOLVED
The solution was change the routes as Cerad says.
Oficial documentation
services.yml
services:
app.hello_controller:
class: AppBundle\Controller\HelloController
routing.yml
hello:
path: /hello
defaults: { _controller: app.hello_controller:indexAction }
Thanks mtaqi and Cerad
The solution was change the routes as Cerad says.
Oficial documentation
services.yml
services:
app.hello_controller:
class: AppBundle\Controller\HelloController
routing.yml
hello:
path: /hello
defaults: { _controller: app.hello_controller:indexAction }
Sorry for the delay and thanks!

Symfony2 - Controller as a Service - Routing via annotations

I am experimenting with creating controllers as services as shown at http://symfony.com/doc/current/cookbook/controller/service.html. I’ve followed this example and everything works fine when I have the route set in app/config/routing.yml. However when I try and set the route via annotations I get an error
My routing.yml file looks like this:
#hello:
# path: /hello/{name}
# defaults: { _controller: app.hello_controller:indexAction }
hello:
resource: "#EventBundle/Controller/HelloController.php"
type: annotation
My controller looks like this:
<?php
namespace Me\EventBundle\Controller;
//use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Bundle\FrameworkBundle\Templating\EngineInterface;
use Symfony\Component\HttpFoundation\Response;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
//class HelloController extends Controller
class HelloController
{
private $templating;
public function __construct(EngineInterface $templating)
{
$this->templating = $templating;
}
/**
* #Route("/hello/{name}", name="hello")
*
*/
public function indexAction($name)
{
return $this->templating->renderResponse(
'EventBundle:Default:test.html.twig',
array('name' => $name)
);
}
}
As I say if I just use routing.yml and not the annotations the page renders correctly. However using annotations I get the error:
Catchable Fatal Error: Argument 1 passed to Me\EventBundle\Controller\HelloController::__construct() must be an instance of Symfony\Bundle\FrameworkBundle\Templating\EngineInterface, none given, called in /Library/WebServer/Documents/symfony-project/app/cache/dev/classes.php on line 2176 and defined
EDIT - as requested in comments:
service.yml looks like:
services:
app.hello_controller:
class: Me\EventBundle\Controller\HelloController
arguments: ['#templating']
The answer with thanks especially to #Artamiel and #Cerad was to add #Route(service="app.hello_controller") just above my class name, so it now looks like:
/**
* #Route(service="app.hello_controller")
*/
class HelloController
{
private $templating;
public function __construct(EngineInterface $templating)
{
$this->templating = $templating;
}
..........etc

Resources