问题
I have a problem with symfony 4. i'm migrating an existing app from symfony 3.3 to symfony 4 and I get an error I can't correct.
here is my controller : BaseController is the controller responding to sm_admin routes
class HomeController extends BaseController
{
public function __construct(ParamBagService $parambag, CacheService $cache) {
parent::__construct($parambag, $cache);
$this->routePrefix = 'sm_adminarea_';
}
/**
* @Route("/", name="adminarea_index")
* @Template("@SMAdmin/Home/index.html.twig")
*/
public function indexAction(Area $area = null)
{
$this->area = $area;
return parent::indexAction();
}
}
here is my routes.yaml
sm_admin:
resource: "@SMAdminBundle/Controller/"
type: annotation
prefix: /
sm_admin_area:
resource: "@SMAdminAreaBundle/Controller/"
type: annotation
prefix: /Area/{area}
When I go to the sm_admin
route, everything is fine. when I go to /area/the_id_of_area
I get the following error : argument 1 passed to indexAction must be of type Area or null, string provided
This lets me think that the area parameter is not converted and the document is not retreived from the database.
I tried adding this paramConverter annotation : @ParamConverter("area", options={"mapping"={"area"="id"}})
but I get the same error...
What am I doing wrong ?
回答1:
ok, I found the solution here https://matthiasnoback.nl/2012/10/symfony2-mongodb-odm-adding-the-missing-paramconverter/
The default paramconverter was using doctrine ORM and I use doctrine ODM so i just had to add ths in the servide.yaml :
param_converter:
class: 'Sensio\Bundle\FrameworkExtraBundle\Request\ParamConverter\DoctrineParamConverter'
arguments: ['@doctrine_mongodb']
tags:
- { name: 'request.param_converter', converter: 'doctrine.odm' }
回答2:
Try using
@ParamConverter("area", class="YourBundle:Area")
as seen http://symfony.com/doc/current/bundles/SensioFrameworkExtraBundle/annotations/converters.html#usage
来源:https://stackoverflow.com/questions/49150205/paramconverter-not-converting-parameters