Symfony搜索功能
我正在尝试实现一种搜索功能,允许用户在特定实体内搜索项目,但我不确定该如何实现这一点,基本上我的小枝文件中有:Symfony搜索功能,symfony,Symfony,我正在尝试实现一种搜索功能,允许用户在特定实体内搜索项目,但我不确定该如何实现这一点,基本上我的小枝文件中有: <form id="form_search" action="{{ path('search') }}"> <input type="text" class="form-control" placeholder="Search" name ="find"> <button type="submit" class="btn btn-defa
<form id="form_search" action="{{ path('search') }}">
<input type="text" class="form-control" placeholder="Search" name ="find">
<button type="submit" class="btn btn-default"></button>
</form>
我想从参数的搜索输入字段中获取输入。但是,他们无法区分用户是刚刚加载了页面还是实际提交了表单。。正如你所看到的,我试图通过将东西分开来了解如何做到这一点:
因此,如果用户已加载页面,则不会进行搜索。。但是,如果用户提交表单,那么它将执行搜索,但由于某些原因,即使在我提交表单时,它也会打印“get”而不是“post”
这是为什么?您需要在控制器中定义一个新函数,用于进行搜索,这是:
class YourController extends Controller
{
/**
* @Route("/", name="index")
*/
public function indexAction(Request $request)
{
return $this->render('Bundle:Search:index.html.twig', array());
}
/**
* @Route("/", name="search")
*/
public function searchAction(Request $request)
{
$your_value = $request->get("find");
//implement your search here,
$repository = $this->getDoctrine()->getRepository('AppBundle:YourEntity');
$result = $repository->findBy(array("field_name"=>$your_value));
//Here you can return your data in JSON format or in a twig template
}
}
不要忘记定义实体类,我建议您看看这个
尝试在您的控制器
$request->getMethod()
中重复此操作,并将method=“post”
添加到您的表单标记中。您说过“您需要定义”:
class YourController extends Controller
{
/**
* @Route("/", name="index")
*/
public function indexAction(Request $request)
{
return $this->render('Bundle:Search:index.html.twig', array());
}
/**
* @Route("/", name="search")
*/
public function searchAction(Request $request)
{
$your_value = $request->get("find");
//implement your search here,
$repository = $this->getDoctrine()->getRepository('AppBundle:YourEntity');
$result = $repository->findBy(array("field_name"=>$your_value));
//Here you can return your data in JSON format or in a twig template
}
}