使用参数修改 symfony 表单的 url
Modify url of a symfony form using parameters
我想使用参数修改提交的 symfony 表单的 url。尝试了该平台的许多解决方案,none 的解决方案有所帮助!
当前url如下:http://localhost:8000/search?app_bundle_search_form%5Bsearch%5D=qui&app_bundle_search_form%5Bbrand%5D=&app_bundle_search_form%5Bprice%5D=500%2C100000&app_bundle_search_form%5B_token%5D=BtA5bZb9HErUXzXFzGFbpEhlD6nD33zr7tKiPLxjpy4
我想要它像`http://localhost:8000/search?search=qui?brand=?minprice=500?maxprice=100000
这是我的控制器:
public function searchAction($searchTerm=null,Request $request)
{
if ($request->getMethod() == 'GET') {
$searchTerm = $request->query->get('app_bundle_search_form')['search'];
$searchBrand = $request->query->get('app_bundle_search_form')['brand'];
$price = $request->query->get('app_bundle_search_form')['price'];
$price = explode(",", $price);
$minPrice = $price[0];
$maxPrice = $price[1];
$em = $this->getDoctrine()->getManager();
$search = $em->getRepository('AppBundle:Classified')->searchClassifieds($searchTerm, $searchBrand, $minPrice, $maxPrice);
}
return $this->render('search-result.html.twig', [
'searchTerm' => $searchTerm,
'results' => $search
]);
}
表格:
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->setMethod('GET')
->add('search', TextType::class,array(
'required'=> false
))
->add('brand',EntityType::class,[
'class'=>Brand::class,
'placeholder'=>'Choose a brand',
'required'=>false,
'query_builder'=>function(BrandRepository $repo){
return $repo->DistinctBrandValue();
}
])
->add('price', TextType::class, array(
'required' => true,
'label' => 'Price',
'attr' => [
'data-slider-min' => '500',
'data-slider-max' => '100000',
'data-slider-step' => '2',
'data-slider-value' => "[500,100000]",
]))
;
}
路由
search:
path: /search
defaults:
_controller: AppBundle:Search:search
requirements:
methods: GET
树枝:
{{ form_start(search, { action: path('search')}) }}
{{ form_widget(search) }}
<button class="btn btn-outline-success my-2 my-sm-0" type="submit">{% trans %}Search{% endtrans %}</button>
<a href="{{ path('search') }}" class="btn btn-primary">{% trans %}Cancel{% endtrans %}</a>
{{ form_end(search) }}
提前致谢!
编辑:
所以我所做的是我必须将表单名称设为空。
public function getBlockPrefix()
{
return '';
}
并将 csrf 保护设置为 false。
public function configureOptions(OptionsResolver $resolver)
{
$resolver->setDefaults([
'csrf_protection' => false,
]);
}
并且还在控制器中将表单名称更改为 null。现在 url 看起来好多了!
您可以覆盖 formType
和 return 中的 getName
方法一个空字符串,例如:
表格:
public function getName()
{
return '';
}
希望对您有所帮助
我想使用参数修改提交的 symfony 表单的 url。尝试了该平台的许多解决方案,none 的解决方案有所帮助!
当前url如下:http://localhost:8000/search?app_bundle_search_form%5Bsearch%5D=qui&app_bundle_search_form%5Bbrand%5D=&app_bundle_search_form%5Bprice%5D=500%2C100000&app_bundle_search_form%5B_token%5D=BtA5bZb9HErUXzXFzGFbpEhlD6nD33zr7tKiPLxjpy4
我想要它像`http://localhost:8000/search?search=qui?brand=?minprice=500?maxprice=100000
这是我的控制器:
public function searchAction($searchTerm=null,Request $request)
{
if ($request->getMethod() == 'GET') {
$searchTerm = $request->query->get('app_bundle_search_form')['search'];
$searchBrand = $request->query->get('app_bundle_search_form')['brand'];
$price = $request->query->get('app_bundle_search_form')['price'];
$price = explode(",", $price);
$minPrice = $price[0];
$maxPrice = $price[1];
$em = $this->getDoctrine()->getManager();
$search = $em->getRepository('AppBundle:Classified')->searchClassifieds($searchTerm, $searchBrand, $minPrice, $maxPrice);
}
return $this->render('search-result.html.twig', [
'searchTerm' => $searchTerm,
'results' => $search
]);
}
表格:
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->setMethod('GET')
->add('search', TextType::class,array(
'required'=> false
))
->add('brand',EntityType::class,[
'class'=>Brand::class,
'placeholder'=>'Choose a brand',
'required'=>false,
'query_builder'=>function(BrandRepository $repo){
return $repo->DistinctBrandValue();
}
])
->add('price', TextType::class, array(
'required' => true,
'label' => 'Price',
'attr' => [
'data-slider-min' => '500',
'data-slider-max' => '100000',
'data-slider-step' => '2',
'data-slider-value' => "[500,100000]",
]))
;
}
路由
search:
path: /search
defaults:
_controller: AppBundle:Search:search
requirements:
methods: GET
树枝:
{{ form_start(search, { action: path('search')}) }}
{{ form_widget(search) }}
<button class="btn btn-outline-success my-2 my-sm-0" type="submit">{% trans %}Search{% endtrans %}</button>
<a href="{{ path('search') }}" class="btn btn-primary">{% trans %}Cancel{% endtrans %}</a>
{{ form_end(search) }}
提前致谢!
编辑: 所以我所做的是我必须将表单名称设为空。
public function getBlockPrefix()
{
return '';
}
并将 csrf 保护设置为 false。
public function configureOptions(OptionsResolver $resolver)
{
$resolver->setDefaults([
'csrf_protection' => false,
]);
}
并且还在控制器中将表单名称更改为 null。现在 url 看起来好多了!
您可以覆盖 formType
和 return 中的 getName
方法一个空字符串,例如:
表格:
public function getName()
{
return '';
}
希望对您有所帮助