简体   繁体   English

使用FOSRestBundle的Symfomy2手动路由定义

[英]Symfomy2 manual route definitions with FOSRestBundle

I am now using the FOSRestBundle in order to build a REST API within my Symfony application. 我现在正在使用FOSRestBundle,以便在Symfony应用程序中构建REST API。 The idea for now is to list some locations(hotels, restaurants...), I managed to configure the automatic routes with FOSRestBundle like: 现在的想法是列出一些位置(酒店,餐厅...),我设法使用FOSRestBundle配置自动路线,例如:

/api/locations , /api/locations/{id} , /api/locations/{name}/detail / 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} / 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 :) 当然,我已经查看了FOSRestBundle的文档以进行手动路由配置,但是由于我花了一些时间尝试进行配置,因此我来这里寻求帮助:)

If you want to manually define a route, it should just be as simple as adding the route to the existing routing configuration. 如果要手动定义路由,则只需将路由添加到现有路由配置中即可。 How exactly you do it depends on how you're handling the routing configuration: annotation, yaml, or xml. 具体执行方式取决于您如何处理路由配置:批注,yaml或xml。

Option 1: YAML 选项1:YAML

In the routing.yml file (ex: src/Vendor/MyBundle/Resources/config/routing.yml ) add something like: routing.yml文件中(例如: src/Vendor/MyBundle/Resources/config/routing.yml ),添加以下内容:

location_nearby:
    pattern:  /api/locations/nearby/{latitude}/{longitude}
    defaults: { _controller: "MyBundle:Location:nearby" }
    requirements:
        _method: GET

which would correspond to this method in LocationController : 这将对应于LocationController此方法:

public function nearbyAction($latitude, $longitude) { ... }

Option 2: Annotations 选项2:注释

Add this use statement to the Controller file: 将此use语句添加到Controller文件:

use FOS\RestBundle\Controller\Annotations\Get;

and then define the route above the controller method: 然后在controller方法上方定义路由:

/**
 * Return a nearby location
 * @Get("/api/locations/nearby/{latitude}/{longitude}")
 */
public function nearbyAction($latitude, $longitude) { ... }

OK here is how to proceed, works fine for me: 好的,这里是如何进行,对我来说很好:

I use the annotation system to route /locations/nearby/{latitude}/{longitude} 我使用注释系统来路由/ 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+"} 然后,我必须使用以下方法指定浮点数: 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: 那些仍将被控制器解释为字符串:“ 64.1333”,我只需要在控制器中使用它即可:

floatval($latitude)

to get url parameters as float and then do my calculations! 将URL参数获取为float,然后进行计算!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM