douhuan1905 2015-12-31 07:10
浏览 14
已采纳

知道在symfony2中传递路由变量超过5的最佳方法吗?

html

<a href="{{path('href="{{path('stockitemdetails_category_report',{'itemcode':itemcode,'category':category,'subcategory':subcategory})}}">

stockregister.yml (Routing file)

 stockregister_report_itemcode_search:
                    pattern:  /stockregister/itemcode/data
                    defaults: { _controller: "EduAssetBundle:StockRegister:stockregisterItemcodeDate" }
                    requirements:
                        itemcode: .+
                        category: .+
                        subcategory: .+
                        type: .+
                        accountunit: .+



     Controller(File) 
 public function stockregisterItemcodeDateAction($itemcode,$category,$type,$accountunit) {}

here is the routing file where i define the variable which i have to pass but the problem is that the variable values is dynamic user can write anything. according to values in controller the dynamic values access and i can run query. but the problem is that the url value will be any thing.

  • 写回答

1条回答 默认 最新

  • doty58493 2015-12-31 17:33
    关注

    The regex .+ is greedy, so it will match everything it finds - including the slash. So itemcode wil match the whole of the path, as the router won't be able to work out whether a slash is part of or the end of the itemcode parameter.

    You could encode the values, so that unencoded slashes indicate the end of a parameter, whereas encoded slashes are part of the parameter value. Then you could remove the regex.

    The better solution is just to use GET, instead of making them part of the path. When you generate a URL using the router, any variables that aren't mapped in the path will be appended as GET variables anyway. If necessary, you could build a ParamConverter to automatically inject the GET variables as parameters into the action.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?