php – 方法redirectToRoute()是否有像render()这样的参数?
发布时间:2020-12-13 18:11:54 所属栏目:PHP教程 来源:网络整理
导读:我需要从symfony2访问twig中的实体. 在控制器内部,我可以做一些事情: return $this-render('frontendBundle::carrodecompras.html.twig',array( 'entity' = $entity)); 然后在twig我可以使用entity.name等访问实体属性. 我需要完成相同的事情,但使用函数red
我需要从symfony2访问twig中的实体.
在控制器内部,我可以做一些事情: return $this->render('frontendBundle::carrodecompras.html.twig',array( 'entity' => $entity )); 然后在twig我可以使用entity.name等访问实体属性. 我需要完成相同的事情,但使用函数redirectToRoute() return $this->redirectToRoute('frontend_carrodecompras',array( 'entity' => $entity,)); 但它似乎没有用. 我收到以下错误: 第32行的frontendBundle :: carrodecompras.html.twig中不存在变量“entity” 编辑:我正在使用Symfony 2.7 变量$entity存在(它实际上在应用程序中称为$cortina我使用$entity进行简化),就在redirectToRoute函数之前,我这样做是为了测试它 echo "<pre>"; var_dump($cortina); echo "</pre>"; return $this->redirectToRoute('frontend_carrodecompras',array( 'cortina' => $cortina,)); 结果如下: object(dexterbackendBundleEntitycortina)#373 (16) { ["id":"dexterbackendBundleEntitycortina":private]=> int(3) ... 这是Twig代码: <tr> {% set imagentela = "img/telas/" ~ cortina.codInterno ~ ".jpg" %} <td><img src="{{ asset(imagentela | lower ) }}" alt="" width="25" height="25"> </td> <td>{{ cortina.nombre }}</td> <td>{{ "$" ~ cortina.precio|number_format('0',','.') }}</td> </tr>
当您从控制器调用redirectToRoute($route,array $parameters)时,$parameters用于生成url标记,而不是用于在视图中呈现的变量,这由分配给您重定向到的路由的控制器完成.
例子: class FirstController extends Controller { /** * @Route('/some_path') */ public function someAction() { // ... some logic $entity = 'some_value'; return $this->redirectToRoute('some_other_route',array('entity' => $entity)); // cast $entity to string } } class SecondController extends Controller { /** * @Route('/some_other_path/{entity}',name="some_other_route") */ public function otherAction($entity) { // some other logic // in this case $entity equals 'some_value' $real_entity = $this->get('some_service')->get($entity); return $this->render('view',array('twig_entity' => $real_entity)); } } (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |