如何檢索我的控制器中通過@ParamConverter聲明爲註釋的所有3個實體(Departement,Category,Advert)直接到我的服務中?如何檢索ParamConverter到服務
我嘗試了很多東西,但沒有正常工作...
謝謝!
控制器
/**
* @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';
}
}
您是否已經實施了參數轉換器並正在運行?你是否需要在控制器或'Breadcrumb'類中由param轉換器創建的對象? – Stepashka