2
votes

Hi I'm developping two applications now with Symfony 3,I have the same problem in both, I want to integrate a search form into twig with render(controller()), the problem is that redirecting to the result page gives me this error

An exception has been thrown during the rendering of a template ("Error when rendering "http://localhost/project/web/app_dev.php/index" (Status code is 302).").

this is my controller

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

this is my function in repository

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


}

this is my view on twig

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

this is the view on twig resultss page

<ul>
	{% for product in listProducts %}
<li>{{ product.name }}</li>
{% endfor %}
</ul>

I need your help how can i solve this problem ?

1
Apparently, you cannot redirect from an embedded controller. Possible duplicate Symfony 2 : 302 http status and exceptionmickdev
Please add your routing configuration for this action.Michal S.

1 Answers

0
votes

Your mistake is here. A comma is too much in the parameters. In general, there is no need to specify Request in the parameters, prototyping by Request $request allows an implicit recovery of the request.

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

Do not forget to add the use to the top of the controller :

use Symfony\Component\HttpFoundation\Request;