Vas*_*ica 1 php forms get symfony
不知道我是否正确地写了这个主题但是无论如何.
由于您可以创建具有不同参数的特定路由,例如:
_search:
pattern: /page/{category}/{keyword}
defaults: { _controller: Bundle:Default:page, category: 9, keyword: null }
Run Code Online (Sandbox Code Playgroud)
有没有办法从使用GET方法的表单到达该路由特定的url格式?
目前网址是/ page?category = 2?keyword = some + keyword
因为您没有注意到这样的路线格式.
我需要做些什么才能让它通过这种特定的格式?我真的不知道如何重写页面网址以匹配特定网址的路由设置.甚至在普通的PHP中偶然发现了......
提前致谢.
这是使用GET方法的HTML表单的默认行为.您需要自己构建该URL.
_search:
pattern: /page/{category}/{keyword}
defaults: { _controller: Bundle:Default:page, category: 9, keyword: null }
_search_endpoint:
pattern: /page
defaults: { _controller: Bundle:Default:redirect }
Run Code Online (Sandbox Code Playgroud)
public function redirectAction()
{
$category = $this->get('request')->query->get('category');
$keyword = $this->get('request')->query->get('keyword');
// You probably want to add some extra check here and there
// do avoid any kind of side effects or bugs.
$url = $this->generateUrl('_search', array(
'category' => $category,
'keyword' => $keyword,
));
return $this->redirect($url);
}
Run Code Online (Sandbox Code Playgroud)
使用Javascript,您可以自己构建URL并在之后重定向用户.
注意:你需要获得自己的查询字符串getter,你可以在这里找到一个Stackoverflow线程,下面我将getQueryString在jQuery对象上使用它.
(function (window, $) {
$('#theFormId').submit(function (event) {
var category, keyword;
event.preventDefault();
// You will want to put some tests here to make
// sure the code behaves the way you are expecting
category = $.getQueryString('category');
keyword = $.getQueryString('keyword');
window.location.href = '/page/' + category + '/' + keyword;
}):
})(window, jQuery);
Run Code Online (Sandbox Code Playgroud)