php 方法redirectToRoute() 可以有像render() 这样的参数吗?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/34775342/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 00:13:22  来源:igfitidea点击:

Can the method redirectToRoute() have arguments like render()?

phpsymfonyredirectrouting

提问by enlego

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

我需要从 symfony2 访问 twig 中的实体。在控制器内部,我可以做一些事情:

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

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

然后在树枝中,我可以使用等访问实体属性entity.name

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

我需要完成同样的事情,但功能 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

在第 32 行的 frontendBundle::carrodecompras.html.twig 中不存在变量“实体”

EDIT: I'm using Symfony 2.7

编辑:我正在使用 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

变量 $entity 存在(在我使用 $entity 进行简化的应用程序中实际上称为 $cortina),就在 redirectToRoute 函数之前,我这样做是为了测试它

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>

回答by Heah

When you call redirectToRoute($route, array $parameters)from a controller, $parametersis 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.

当您redirectToRoute($route, array $parameters)从控制器调用时,$parameters用于生成 url 标记,而不是要在视图中呈现的变量,这是由分配给您重定向到的路由的控制器完成的。

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));
    }
}

回答by SacWebDeveloper

$this->redirectToRoute('something', array('id' => 1)is a convenience wrapper to $this->redirect($this->generateUrl('something', array('id' => 1))). It builds a URL with your params and is expecting the value of the params to be a string or a number.

$this->redirectToRoute('something', array('id' => 1)是一个方便的包装器$this->redirect($this->generateUrl('something', array('id' => 1)))。它使用您的参数构建一个 URL,并期望参数的值是字符串或数字。

http://symfony.com/blog/new-in-symfony-2-6-new-shortcut-methods-for-controllers

http://symfony.com/blog/new-in-symfony-2-6-new-shortcut-methods-for-controllers

You need to either pass the id of the entity to then fetch the data in the new action or break it down into individual pieces of data before it hits the redirectToRoute() call.

您需要传递实体的 id,然后在新操作中获取数据,或者在它点击 redirectToRoute() 调用之前将其分解为单独的数据片段。

class MyController extends Controller {
    public function myAction(){
        $cortina = new Cortina();
        $cortina->something = "Some text";

        $em = $this->getDoctrine()->getManager();
        $em->persist($cortina);
        $em->flush();

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