用get方法将路由url格式传递给symfony2格式 [英] Passing route url format to symfony2 form with get method

查看:127
本文介绍了用get方法将路由url格式传递给symfony2格式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



由于您可以创建具有不同参数的特定路线,例如:

  _search:
模式:/ page / {category} / {keyword}
默认值:{_controller:Bundle:Default:page,category: 9,关键字:null}

有没有什么办法可以从GET方法的表单到达那条路线具体的网址格式?



目前网址是/ page?category = 2?keyword = some + keyword



我需要做些什么才能使它通过这种特定的格式?我真的不知道如何重写页面url来匹配特定url的路由设置。即使在普通的PHP中,也偶然发现了这一点......



感谢您的提前致谢。

解决方案

div>

这是使用GET方法的HTML表单的默认行为。您需要自行构建该网址。



后端




  • 缺点:它向服务器发出两个请求,而不是一个

  • 优点:由于URL是使用路由服务构建的,所以它更易于维护



您的路由文件



  _search:
模式:/ page / {category} / {关键字}
默认值:{_controller:Bundle:Default:page,category:9,keyword:null}

_search_endpoint:
pattern:/ page
defaults: {_controller:Bundle:Default:redirect}



您的控制器



  public function redirectAction()
{
$ category = $ this-> get('request') - > query-> get ('类别');
$ keyword = $ this-> get('request') - > query-> get('keyword');

//你可能想在这里添加一些额外的支票,那里
//可以避免任何类型的副作用或错误。

$ url = $ this-> generateUrl('_ search',array(
'category'=> $ category,
'keyword'=> $ keyword,
));

返回$ this->重定向($ url);
}



前端路线



使用Javascript,您可以自己创建URL并在之后重定向用户。





注意:您将需要获取自己的查询字符串getter,您可以找到Stackoverflow thread here ,下面我将使用 getQueryString 在jQuery对象上。

 (function(window,$){
$(' #theFormId')。submit(function(event){
var category,keyword;

event.preventDefault();

//你会想把一些测试在这里做
//确定代码的行为与你期待的一样b
$ b category = $ .getQueryString('category');
keyword = $ .getQueryString('keyword');

window.location.href ='/ page /'+ category +'/'+ keyword;
}):
})(window,jQuery);


Not sure if I properly wrote the subject but anyway.

Since you can create specific routes with different parameters for eg:

_search:
    pattern: /page/{category}/{keyword}
    defaults: { _controller: Bundle:Default:page, category: 9, keyword: null }

is there any way from a form with GET method to get to that route specific url format?

At the moment the url is like /page?category=2?keyword=some+keyword

As such is not passing to the route format as you may noticed.

What do I need to do to get it working through this specific format? I really have no idea how to rewrite the page url to match the route settings for the specific url. Even in plain php was stumbled on this ...

Thanks in advance.

解决方案

It's the default behavior of HTML forms with GET method. You will need to build that URL yourself.

Backend way

  • Drawback: It makes two requests to the server instead of one
  • Advantage: It's more maintainable because the URL is built using the routing service

Your routing file

_search:
    pattern: /page/{category}/{keyword}
    defaults: { _controller: Bundle:Default:page, category: 9, keyword: null }

_search_endpoint:
    pattern: /page
    defaults: { _controller: Bundle:Default:redirect }

Your controller

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

Frontend way

Using Javascript, you can build the URL yourself and redirect the user afterwards.

  • Drawback: You don't have access to the routing service (although you could use the FOSJsRoutingBundle bundle)
  • Advantage: You save one request

Note: You will need to get your own query string getter you can find a Stackoverflow thread here, below I'll use getQueryString on the jQuery object.

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

这篇关于用get方法将路由url格式传递给symfony2格式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆