Symfony4:渲染无法传递参数
Symfony4: render cannot pass parameters
从Symfony2.X更新到Symfony 4.4并验证运行后,发现Twig不识别模态
由于不识别modal,所以即使if语句中不以modal显示的代码以modal显示也会显示。
当我尝试“if modal is empty”时,显示的是if语句中的代码,所以好像modal没有通过
通过模式的代码随着 Symfony 的更新而发生了变化。这可能有影响。
你有什么想法吗?
Old_code
<div id="imageDialog" title="Image management">
{{ render(controller("AppBundle:Shop/Image:manager", {"modal": true})) }}
</div>
Changed_code
<div id="imageDialog" title="Image management">
{{ render(controller("AppBundle\Controller\Shop\ImageController::managerAction", {"modal": true})) }}
</div>
有问题的代码
<div class="tabpanel selected" id="imageFolder" >
<div class="tabcontent">
<div class="operations">
{% if not modal %}
<div class="pull-right">
<form method="post" action="{{ path('app_shop_image_delete') }}">
<input type="hidden" name="methods" value="delete">
<input type="hidden" name="_csrf_token" value="{{ csrf_token("authenticate") }}">
<input type="hidden" name="ids" value="[]">
<button type="submit" class="delete btn btn-danger"><i class="icon-trash"></i> Delete</button>
</form>
</div>
{% endif %}
</div>
</div>
</div>
图像控制器
*
* @Method("GET")
* @Route("/manager")
*
* @Template("@AppBundle/Shop/Image/manager.html.twig")
*/
public function managerAction(Request $request)
{
$routeParams = $request->get('_route_params');
$uploadUrl = $this->generateUrl("app_shop_image_save");
return array(
'modal' => isset($routeParams["modal"]) ? $routeParams["modal"] : false,
'form' => $this->createUploadForm($uploadUrl)->createView(),
);
}
用下面的方法效果很好。
我无法获取$routeParams["modal"]
,但如果有办法获取,请告诉我。
public function managerAction(Request $request)
{
$routeParams = $request->query->get('modal');
$uploadUrl = $this->generateUrl("app_hq_image_save");
return array(
'modal' => isset($routeParams) ? $routeParams : false,
'form' => $this->createUploadForm($uploadUrl)->createView(),
);
}
<div id="imageDialog" title="Image management">
{{ render(controller("AppBundle\Controller\Shop\ImageController::managerAction", {}, {"modal": true})) }}
</div>
public function managerAction(Request $request)
{
$isModal = $request->query->get('modal') != null ? $request->query->get('modal') : false;
$uploadUrl = $this->generateUrl("app_hq_image_save");
return array(
'modal' => $isModal,
'form' => $this->createUploadForm($uploadUrl)->createView(),
);
}
但我想知道您是否不应该将模态作为具有默认值 false
的真实路由参数。
https://symfony.com/doc/4.4/routing.html#optional-parameters
从Symfony2.X更新到Symfony 4.4并验证运行后,发现Twig不识别模态
由于不识别modal,所以即使if语句中不以modal显示的代码以modal显示也会显示。
当我尝试“if modal is empty”时,显示的是if语句中的代码,所以好像modal没有通过
通过模式的代码随着 Symfony 的更新而发生了变化。这可能有影响。
你有什么想法吗?
Old_code
<div id="imageDialog" title="Image management">
{{ render(controller("AppBundle:Shop/Image:manager", {"modal": true})) }}
</div>
Changed_code
<div id="imageDialog" title="Image management">
{{ render(controller("AppBundle\Controller\Shop\ImageController::managerAction", {"modal": true})) }}
</div>
有问题的代码
<div class="tabpanel selected" id="imageFolder" >
<div class="tabcontent">
<div class="operations">
{% if not modal %}
<div class="pull-right">
<form method="post" action="{{ path('app_shop_image_delete') }}">
<input type="hidden" name="methods" value="delete">
<input type="hidden" name="_csrf_token" value="{{ csrf_token("authenticate") }}">
<input type="hidden" name="ids" value="[]">
<button type="submit" class="delete btn btn-danger"><i class="icon-trash"></i> Delete</button>
</form>
</div>
{% endif %}
</div>
</div>
</div>
图像控制器
*
* @Method("GET")
* @Route("/manager")
*
* @Template("@AppBundle/Shop/Image/manager.html.twig")
*/
public function managerAction(Request $request)
{
$routeParams = $request->get('_route_params');
$uploadUrl = $this->generateUrl("app_shop_image_save");
return array(
'modal' => isset($routeParams["modal"]) ? $routeParams["modal"] : false,
'form' => $this->createUploadForm($uploadUrl)->createView(),
);
}
用下面的方法效果很好。
我无法获取$routeParams["modal"]
,但如果有办法获取,请告诉我。
public function managerAction(Request $request)
{
$routeParams = $request->query->get('modal');
$uploadUrl = $this->generateUrl("app_hq_image_save");
return array(
'modal' => isset($routeParams) ? $routeParams : false,
'form' => $this->createUploadForm($uploadUrl)->createView(),
);
}
<div id="imageDialog" title="Image management">
{{ render(controller("AppBundle\Controller\Shop\ImageController::managerAction", {}, {"modal": true})) }}
</div>
public function managerAction(Request $request)
{
$isModal = $request->query->get('modal') != null ? $request->query->get('modal') : false;
$uploadUrl = $this->generateUrl("app_hq_image_save");
return array(
'modal' => $isModal,
'form' => $this->createUploadForm($uploadUrl)->createView(),
);
}
但我想知道您是否不应该将模态作为具有默认值 false
的真实路由参数。
https://symfony.com/doc/4.4/routing.html#optional-parameters