如何将ParamConverter检索到服务中

时间:2016-12-16 22:47:48

标签: php symfony

如何通过我的控制器中的@ParamConverter将我声明为注释的所有3个实体(Departement,Category,Advert)直接检索到我的服务中?

我尝试过很多东西,但没有什么能正常工作......

谢谢!

控制器

  /**
    * @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';
    }

}

2 个答案:

答案 0 :(得分:1)

以下是我对如何做到这一点的看法(未经测试!)

首先,您应该从关联数组中返回控制器中的所有实体。

之后,您应该在服务中实施Symfony\Component\EventDispatcher\EventSubscriberInterface。所以它看起来像这样:

<强> 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 }

答案 1 :(得分:0)

首先,我建议实施3个转换器并将它们与树枝延伸分开。对于一个班级来说太过分了。

二。将您的服务订阅为内核控制器事件的​​事件监听器。在此阶段,请求会在$event->getRequest()->attributes

中包含您需要的所有数据