Sébastien Tetard
Sébastien Tetard

Reputation: 29

How to retrieve ParamConverter into a service

How to retrieve all my 3 entities (Departement, Category, Advert) declared as annotation via @ParamConverter in my controller directly into my service ?

I tried many stuff, but nothing works properly...

Thanks !

Controller

  /**
    * @Template("frontend/advertisement/show.html.twig")
    * @ParamConverter("department", options={"mapping": {"slug_department": "department_slug"}})
    * @ParamConverter("category", options={"mapping": {"slug_category": "slug_fr"}})
    * @ParamConverter("advert", options={"mapping": {"slug_advert": "slug"}})
    * @Route(
    *     "/{_locale}/annonces/{slug_department}/{slug_category}/{slug_advert}",
    *     name="advertShow",
    *     requirements={
    *         "_locale": "en|fr"
    *     }
    * )    */
    public function showAction(Request $request, Department $department , Category $category , Advert $advert  )
    {
        return [
            "advert"=>$advert
        ];
    }

Service.yml

 app.breadcrumb:
        class:     AppBundle\Service\Breadcrumb
        arguments: ['@router']
        tags:
            -  { name: twig.extension }
            - { name: request.param_converter, priority: 20 }

Service.php

namespace AppBundle\Service;

use Symfony\Bundle\FrameworkBundle\Routing\Router;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\ParamConverter;
use Sensio\Bundle\FrameworkExtraBundle\Request\ParamConverter\ParamConverterInterface;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\RequestStack;

class Breadcrumb extends \Twig_Extension implements ParamConverterInterface
{

    protected $router; 

    public function __construct(Router $route)
    {
        $this->router = $router;
    }

    public function generate($path)
      {

        return ["Home", "Item 1 ", "Item 2"];
      }

    public function getFunctions()
    {
        return array(
            'breadcrumb' => new \Twig_Function_Method($this, 'generate')
        );
    }

    function supports(ParamConverter $configuration)
    {   

    }

    function apply(Request $request, ParamConverter $configuration)
    {
    }

    public function getName()
    {
        return 'Breadcrumb';
    }

}

Upvotes: 0

Views: 957

Answers (2)

Stepashka
Stepashka

Reputation: 2698

First of whole I'd recommend to implement 3 converters and separate them from twig extension. It is going to be too much for one class.

Second. Subscribe your service as event listener to kernel controller event. At this stage the request have all data you need in $event->getRequest()->attributes

Upvotes: 0

Denis V
Denis V

Reputation: 3390

Here is my vision on how it could be done (untested!)

First of all, you should return all the entities from your controller in an associated array.

After that you should implement Symfony\Component\EventDispatcher\EventSubscriberInterface in your service. So that it looks like this:

Service.php

namespace AppBundle\Service;

use Symfony\Component\EventDispatcher\EventSubscriberInterface;
// .. other imports

class Breadcrumb extends \Twig_Extension implements ParamConverterInterface, EventSubscriberInterface
{
    private $entities = array();
    // .. other fields ..

    public static function getSubscribedEvents()
    {
        return array(
            KernelEvents::VIEW => 'onKernelView',
        );
    }

    public function onKernelView(GetResponseForControllerResultEvent $event)
    {
        $view = $event->getControllerResult();
        if (is_array($view) && array_key_exists('entities', $view)) {
            $this->entities = $view;
        }
    }

    // .. other methods ..
}

Service.yml

app.breadcrumb:
    class:     AppBundle\Service\Breadcrumb
    arguments: ['@router']
    tags:
        - { name: twig.extension }
        - { name: request.param_converter, priority: 20 }
        - { name: kernel.event_listener, event: kernel.view, method: onKernelView, priority: 101 }

Upvotes: 1

Related Questions