symfony2中有规律的动态路由
2016-01-14 17:41
573 查看
比如我现在有3个路由
大家会发现每个path都有共同的部分 /goods/list_by_
这其实在symfony2中的文档中其实提出了解决的方法
路由方面这么写
控制器中这么写
mall_goods_list_by_price_or_sales_or_create_time: path: /goods/list_by_price defaults: {_controller:MallBundle:Goods:listByPrice}
mall_goods_list_by_price_or_sales_or_create_time: path: /goods/list_by_sales defaults: {_controller:MallBundle:Goods:listBySales }
mall_goods_list_by_price_or_sales_or_create_time: path: /goods/list_by_create_time defaults: {_controller:MallBundle:Goods:listByCreateTime}
大家会发现每个path都有共同的部分 /goods/list_by_
这其实在symfony2中的文档中其实提出了解决的方法
路由方面这么写
mall_goods_list_by_price_or_sales_or_create_time: path: /goods/list_by_{condition} (这就相当于一个占位符了) defaults: {_controller:MallBundle:Goods:listByCondition} requirements: {condition: price|sales|create_time} (这么写是为了告诉框架,只有有其中3个值的之一我才有这个路径)
控制器中这么写
/** * 按所给条件(价格,销售值,时间)获取商品列表 */ public function listByConditionAction(request $request, $condition) { $warehouseId = trim($request->request->get('categoryId')); $page = (int) $request->request->get('page'); $order = trim($request->request->get('order')); if (empty($order)) { $order = 'DESC'; } if ($page < 1) { $page = 1; } $pageSize = (int) $request->request->get('$pageSize'); if (!in_array($pageSize, array(8, 16))) { $pageSize = 8; } $sort_field = array( 'price' => 'minPrice', 'create_time' => 'createTime', 'sales' => 'sales', ); $repository = $this->getDoctrine()->getRepository('AppBundle:Goods'); $goodsList = $repository->findBy( array( 'warehouseId' => $warehouseId, 'status' => 0, ), array($sort_field[$condition] => $order ), $pageSize, $pageSize * ($page - 1) ); $ret = $this->_getListByCondition($warehouseId, $goodsList); return new JsonResponse($ret); }
相关文章推荐
- 多线程参数传递
- easyui DataGrid 工具类之 后台生成列
- APICloud:让开发移动应用像拼积木一样简单
- each方法应用(列表-json数据展示)
- 【matconvnet】故障排除:Error using mex nvcc fatal : Unsupported gpu architecture 'compute_52'
- 搭配Android Studio的新工具,完虐自带模拟机型
- 实现Android插件化的核心技术简介(二):Android dynamic load resources
- 计算图像均值
- struts2 中,@NameSpace 和 @Action 的使用示例
- 正方形字符串
- maven引入本地JAR包
- 蓝懿IOS新控件UIDateSpickers/UIAlertController
- windows2003+SQL server2005群集-故障转移
- iOS开发之多线程的五种方法
- maven hello world: 使用maven前的一些配置和命令行创建一个webapp项目
- RTP Payload Type 列表
- Opencv图片转视频
- Redhat的源代码在哪里下载
- 写给那些让我糊里糊涂的HTTP、TCP、UDP、Socket
- C# .NET web 实现邮件发送