Reputation: 23
In my bundle I need to initialize my doctrine manager class (as a service and using ManagerRegistry) in constructor of controller, but symfony still throws this exception:
Type error: Too few arguments to function AdminBundle\Controller\RegistraceController::__construct(), 0 passed in C:\apache\htdocs\mujProjekt\vendor\symfony\symfony\src\Symfony\Component\HttpKernel\Controller\ControllerResolver.php on line 198 and exactly 1 expected
Controller:
namespace AdminBundle\Controller;
use AdminBundle\Manager\AdminManager;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
/**
* Class DefaultController
* @package AdminBundle\Controller
* @Route("/registrace")
*/
class RegistraceController extends Controller
{
/**
* @var AdminManager
*/
private $manager;
public function __construct(AdminManager $manager)
{
$this->manager = $manager;
}
...
AdminManager:
namespace AdminBundle\Manager;
use AdminBundle\Entity\Uzivatel;
use Doctrine\Common\Persistence\ManagerRegistry;
class AdminManager
{
private $em;
public function __construct(ManagerRegistry $Doctrine)
{
$this->em = $Doctrine->getManager('default');
}
...
AdminBundle\Resources\config\services.yml :
services:
# admin.example:
# class: AdminBundle\Example
# arguments: ["@service_id", "plain_value", "%parameter%"]
admin.admin_manager:
class: AdminBundle\Manager\AdminManager
arguments:
["@doctrine"]
I tried to clear cache, but no success. The services.yml from AdminBundle is correctly included in config.yml.
orm config in config.yml:
orm:
auto_generate_proxy_classes: '%kernel.debug%'
default_entity_manager: default
entity_managers:
default:
connection: default
mappings:
AdminBundle: ~
I'm using Symfony 3.3 and PHP 7.1.
Upvotes: 0
Views: 1020
Reputation: 23
Thank you all for your replies. Fortunately I solved my problem by adding this to services in app/config/services.yml.
AdminBundle\Controller\:
resource: '%kernel.project_dir%/src/AdminBundle/Controller'
public: true
tags: ['controller.service_arguments']
Upvotes: 0
Reputation: 61
if you want to inject your AdminManager in your RegistraceController, you have to define the RegistraceController as a service. Look at https://symfony.com/doc/current/controller/service.html. There are some drawbacks of this approach, because you do not inherit from Symfony‘s base Controller. So, you have to inject the Router and the Template Engine too, if you need them. But I like defining my Controller as services. It‘s much cleaner when you see dependencies.
Instead of this, you can use the Symfony Container inside your controller as an Inversion Of Controll Container and get your service with $this->get('admin.admin_manager'); from inside your action.
Upvotes: 1
Reputation: 236
So i think your service yml need to look like that:
services:
admin.admin_manager:
class: AdminBundle\Manager\AdminManager
arguments: ["@doctrine"]
admin.admin_controller:
class: AdminBundle\Controller\RegistraceController
arguments: ["@admin.admin_manager"]
Look up here Symfony Service Container
Hope it will help!
Greetings :)
Upvotes: 0