1
1
1alexandr2015-01-07 18:57:36
symfony
1alexandr, 2015-01-07 18:57:36

symfony2. How to properly use a repository in a controller?

Hello dear experts. Please tell me what I'm doing wrong and how to overcome this error:

Catchable Fatal Error: Argument 1 passed to Allergo\MainBundle\Controller\HomePageController::__construct() must be an instance of Allergo\MainBundle\Entity\CategoryRepository, none given, called in /var/www/allergo.loc/app/cache/dev/classes.php on line 2456 and defined

and here is the code itself:
.../Resourse/config/controllers.yml
services:

    allergo_main.home_page_controller:
        class: Allergo\MainBundle\Controller\HomePageController
        arguments: [ @repository.category ]

../Resource/config/repositories.yml
services:
    repository.category:
        class: Allergo\MainBundle\Entity\CategoryRepository
        factory_service: doctrine.orm.entity_manager
        factory_method: getRepository
        arguments: [ Allergo\MainBundle\Entity\Category ]

# .../Resourse/config/services.yml
imports:
    - { resource: repositories.yml }
    - { resource: controllers.yml }

services:
#    allergo_main.example:
#        class: Allergo\MainBundle\Example
#        arguments: [@service_id, "plain_value", %parameter%]

../Controller/HomePageController.php
/**
     * @var CategoryRepository
     */
    private $categoryRepository;

    /**
     * @param CategoryRepository $categoryRepository
     */
    public function __construct
    (
        CategoryRepository $categoryRepository
    )
    {
        $this->categoryRepository = $categoryRepository;
    }

app/config/config.yml
imports:
    - { resource: "@AllergoMainBundle/Resources/config/services.yml" }
    - { resource: parameters.yml }
    - { resource: security.yml }

....

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
Alex, 2015-01-07
@shoomyst

You have a controller as a service, you still need to look at the routing.
And it would be nice to look at the stack trace

B
BoShurik, 2015-01-08
@BoShurik

Similar question
symfony.com/doc/current/cookbook/controller/servic...
In the route settings, replace
on the

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question