duanhuanyou6478 2014-03-24 00:04
浏览 23
已采纳

使用FOSRestBundle的Symfomy2手动路由定义

I am now using the FOSRestBundle in order to build a REST API within my Symfony application. The idea for now is to list some locations(hotels, restaurants...), I managed to configure the automatic routes with FOSRestBundle like:

/api/locations , /api/locations/{id} , /api/locations/{name}/detail

with this controller:

class LocationController extends FOSRestController implements ClassResourceInterface
{

/**
 * GET /locations 
 *
 * @return Array
 *
 */
public function cgetAction()
{
    $locations = $this->getDoctrine()
              ->getManager()
              ->getRepository('VisitBILocationsBundle:Location')
              ->findAll(); 

    if (!$locations) {
        return array(
            'locations' => $locations,
            'status' => 1
        );
    }

    return array(
            'locations' => $locations,
            'status' => 0
        );
}

/**
 * GET /locations/{locationId} 
 *
 * @return Array
 *
 */
public function getAction($id)
{
    $location = $this->getDoctrine()
              ->getManager()
              ->getRepository('VisitBILocationsBundle:Location')
              ->findBy(array('id' => $id));

    if (!$location) {
        return array(
            'location' => $location,
            'status' => 1
        );
    }

    return array(
            'location' => $location,
            'status' => 0
    );       
}

/**
 * GET /locations/{name}/detail 
 *
 * @return Array
 */
public function getDetailAction($name)
{

    $detail = $this->getDoctrine()
             ->getManager()
             ->getRepository('VisitBILocationsBundle:LocationDetail')
             ->findBy(array('name' => $name));

    if (!$detail) {
        return array(
            'locationDetail' => $detail,
            'status' => 1
        );
    }

    return array(
            'locationDetail' => $detail,
            'status' => 0
    );      
}


}

I've been struggling with this, but would anyone know how should I proceed to generate one custom url like this:

/api/locations/nearby/{latitude}/{longitude}

The idea is that I would provide my own latitude and longitude, and the backend will calculate and provide the locations which are the closest to me.

Of course I've looked at the documentation of FOSRestBundle for manual route configuration, but since I spent some time trying to do it, I come here to ask for some help :)

  • 写回答

2条回答 默认 最新

  • dqzpt40064 2014-03-24 23:03
    关注

    OK here is how to proceed, works fine for me:

    I use the annotation system to route /locations/nearby/{latitude}/{longitude}

    /**
     * Return a nearby location
     * @Get("/locations/nearby/{latitude}/{longitude}", requirements={"latitude" = "[-+]?(\d*[.])?\d+", "longitude" = "[-+]?(\d*[.])?\d+"})
     */
    public function nearbyAction($latitude, $longitude) {...}
    

    Then I have to specify float numbers with: requirements={"latitude" = "[-+]?(\d*[.])?\d+", "longitude" = "[-+]?(\d*[.])?\d+"}

    Those will still be interpreted as string by the controller: "64.1333", I just have to use this in the controller:

    floatval($latitude)
    

    to get url parameters as float and then do my calculations!

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥60 求一个简单的网页(标签-安全|关键词-上传)
  • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法
  • ¥15 基于卷积神经网络的声纹识别
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP