enl*_*ego 7 php redirect routing symfony
我需要从symfony2访问twig中的实体.在控制器内部,我可以做一些事情:
return $this->render('frontendBundle::carrodecompras.html.twig', array(
'entity' => $entity
));
Run Code Online (Sandbox Code Playgroud)
然后在树枝上我可以用entity.name这样的方式访问实体属性.
我需要完成相同的功能,但功能 redirectToRoute()
return $this->redirectToRoute('frontend_carrodecompras', array(
'entity' => $entity,
));
Run Code Online (Sandbox Code Playgroud)
但它似乎没有用.
我收到以下错误:
第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,
));
Run Code Online (Sandbox Code Playgroud)
结果如下:
object(dexter\backendBundle\Entity\cortina)#373 (16) {
["id":"dexter\backendBundle\Entity\cortina":private]=>
int(3)
...
Run Code Online (Sandbox Code Playgroud)
这是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>
Run Code Online (Sandbox Code Playgroud)
Hea*_*eah 16
当您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));
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
33180 次 |
| 最近记录: |