【发布时间】:2017-02-10 18:37:23
【问题描述】:
您好,我现在正在使用 Symfony 3 开发两个应用程序,两者都有相同的问题,我想将搜索表单集成到带有渲染(控制器())的树枝中,问题是重定向到结果页面给出我这个错误
在渲染模板期间引发了异常(“渲染时出错“http://localhost/project/web/app_dev.php/index”(状态代码为 302)。”)。
这是我的控制器
class ProductController extends Controller
{
public function resultsAction($criteria){
$em=$this->getDoctrine()->getManager();
$listProducts = $em->getRepository('ProjectProductBundle:Product')->getListBy($criteria);
return $this->render('ProjectFrontBundle:Front:results.html.twig', array('listProducts'=>$listProducts));
}
public function SearchByNameAction(Request $request){
$product = new Product();
$form=$this->get('form.factory')->create(ProductType::class,$product);
if($request->isMethod('post') && $form->handleRequest($request)->isValid()){
$em=$this->getDoctrine()->getManager();
$criteria = $form["name"]->getData();
return $this->redirectToRoute('project_product_results', array('criteria'=>$criteria));
}
return $this->render('ProductFrontBundle:Front:search.html.twig',array('form'=>$form->createView()));
}
}
这是我在存储库中的功能
class ProductRepository extends \Doctrine\ORM\EntityRepository
{
public function getListBy($criteria)
{
$qb = $this->createQueryBuilder('p')
->where('p.name LIKE :criteria')
->setParameter('criteria', '%'.$criteria.'%');
return $qb->getQuery()->getResult();
}
}
这是我对树枝的看法
<div>
{% block search_body %}
{{ render(controller('ProductProductBundle:Product:SearchByName',{'request': app.request,})) }}
{% endblock %}
</div>
这是树枝结果页面上的视图
<ul>
{% for product in listProducts %}
<li>{{ product.name }}</li>
{% endfor %}
</ul>
我需要您的帮助,我该如何解决这个问题?
【问题讨论】:
-
显然,您不能从嵌入式控制器重定向。可能重复Symfony 2 : 302 http status and exception
-
请为此操作添加您的路由配置。
标签: javascript php html symfony twig