【问题标题】:Redirection when rendering controller in twig SYMFONY 302在 twig SYMFONY 302 中渲染控制器时重定向
【发布时间】: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>

我需要您的帮助,我该如何解决这个问题?

【问题讨论】:

标签: javascript php html symfony twig


【解决方案1】:

你的错误就在这里。 参数中的逗号过多。一般来说,不需要在参数中指定 Request,通过 Request $request 进行原型设计允许隐式恢复请求。

<div>
    {% block search_body %}
        {{ render(controller('ProductProductBundle:Product:SearchByName')) }}
    {% endblock %}
</div>

不要忘记在控制器顶部添加使用:

use Symfony\Component\HttpFoundation\Request;

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-03-01
    • 2015-11-25
    • 1970-01-01
    • 1970-01-01
    • 2016-11-29
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多