Bogdan Ivanov
Bogdan Ivanov

Reputation: 202

Call function in another class Symfony2

I have a function that I need a few controllers. Can i call it in some other way than to extend the class in which the function is? Question:

class CategoryController extends Controller
{
    public function getCategoriesAction()
    {
        $categories = $this->getDoctrine()->getRepository('ModelBundle:Category')->findAll();
        return $categories;
    }
}

How call this function in PostController?

Upvotes: 0

Views: 6257

Answers (4)

limitium
limitium

Reputation: 260

You can also use pure PHP with traits

trait CategoryProvider {
   public function getCategoriesAction() {
    $categories = $this->container->get('dummy.manager')>getCategories();
    //...
   }
}

After that you can use this trait in your controllers

class Controller1 {

   use CategoryProvider;

   public function indexAction(){
      //...
      $this->getCategoriesAction();
      //...
   }
}

class Controller2 {

   use CategoryProvider;

   public function anotherAction(){
      //...
      $this->getCategoriesAction();
      //...
   }
}

Upvotes: 1

Cristian Bujoreanu
Cristian Bujoreanu

Reputation: 1167

Another approach, how @Med mentioned, is to declare your controller as service:

services:
    categoryController:
        class: AppBundle\Controller\CategoryController
        arguments:
            entityManager: @entityManager

Upvotes: 0

Peter Bailey
Peter Bailey

Reputation: 105868

You should not define model-style getters on a controller. The notion of a "getCategoriesAction" method for a controller should be vacated from your brain.

What you want to do is define this fetching behavior in the model layer. This can be done a bunch of different ways. Cristian just posted one way so I'll offer another.

You can define the Entity Repository itself as a service.

app/config/services.yml

services:
    model.repository.category:
        class:   ModelBundle\Entity\CategoryRepository
        factory: ["@doctrine.orm.entity_manager", getRepository]
        arguments: ['ModelBundle:Category']

Then in your controller (assuming it's a container-aware controller) you can do this

$categories = $this->get('model.repository.category')->findAll();

Upvotes: 0

Cristian Bujoreanu
Cristian Bujoreanu

Reputation: 1167

You can define a service and inject entity manager in it, for retrieving data:

dummy.manager:
    class: AppBundle\Model\DummyManager
    arguments:
        entityManager: "@doctrine.orm.entity_manager"

In your service, call findAll() method:

class DummyManager {

   /** @var EntityManager */
   protected $entityManager;

   public function __construct($entityManager) {
       $this->entityManager = $entityManager;
   }

   public function getCategories(){ 
      return $this->entityManager->getRepository('ModelBundle:Category')->findAll();
   }
}

And last step, in your controller:

class CategoryController extends Controller
{
    public function getCategoriesAction()
    {
        $categories = $this->container->get('dummy.manager')->getCategories();
        //...
    }
}

I hope this helps.

Upvotes: 1

Related Questions