duanniedang3946 2016-01-13 19:25
浏览 58
已采纳

方法redirectToRoute()是否有像render()这样的参数?

I need to access an entity in twig from symfony2. Inside the controler, I can do something as:

return $this->render('frontendBundle::carrodecompras.html.twig', array(
        'entity' => $entity
));

And then in twig I can access the entity properties with entity.name and such.

I need to accomplish the same thing but with the function redirectToRoute()

return $this->redirectToRoute('frontend_carrodecompras', array(
        'entity' => $entity,
));

But it doesn't seem to work.

I'm getting the following error:

Variable "entity" does not exist in frontendBundle::carrodecompras.html.twig at line 32

EDIT: I'm using Symfony 2.7

The variable $entity exists (it's actually called $cortina in the app I was using $entity for simplification), just before the redirectToRoute function I did this to test it

echo "<pre>";
var_dump($cortina);
echo "</pre>";

return $this->redirectToRoute('frontend_carrodecompras', array(
                'cortina' => $cortina,
                ));

And the result is this:

object(dexter\backendBundle\Entity\cortina)#373 (16) {
  ["id":"dexter\backendBundle\Entity\cortina":private]=>
  int(3)
  ...

This is the Twig code:

<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>
  • 写回答

2条回答 默认 最新

  • douzaoqu4447 2016-01-13 19:59
    关注

    When you call redirectToRoute($route, array $parameters) from a controller, $parameters is used to generate the url tokens, not variables to render in view, this is done by the controller assigned to the route you are redirecting to.

    example :

    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));
        }
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 35114 SVAC视频验签的问题
  • ¥15 impedancepy
  • ¥15 在虚拟机环境下完成以下,要求截图!
  • ¥15 求往届大挑得奖作品(ppt…)
  • ¥15 如何在vue.config.js中读取到public文件夹下window.APP_CONFIG.API_BASE_URL的值
  • ¥50 浦育平台scratch图形化编程
  • ¥20 求这个的原理图 只要原理图
  • ¥15 vue2项目中,如何配置环境,可以在打完包之后修改请求的服务器地址
  • ¥20 微信的店铺小程序如何修改背景图
  • ¥15 UE5.1局部变量对蓝图不可见