2016-11-18 46 views
0

我正在關注API版本的FOSRest documentation如何調試需要接受頭文件的路由?

但是,我不清楚標題如何使用media_type將它連接到特定的控制器。

現在,我發送了頭:用Chrome擴展郵差Accept: application/json;version=1.0/api/user/status

但是我得到一個錯誤No route found for "POST /api/user/status"

這裏是我的配置:

的routing.yml :

type: rest 
prefix: /api 
resource: Acme\Bundle\SomeBundle\Controller\DefaultController 

DefaultController.php:

use FOS\RestBundle\Controller\Annotations\Version; 

/** 
* @Version("1.0") 
* @RouteResource("User", pluralize=false) 
*/ 
class User 

... 

public function postStatusAction() 

config.yml

fos_rest: 
    versioning: 
     enabled: true 
     resolvers: 
      query: false 
      custom_header: false 
      media_type: 
       enabled: true 
       regex: '/(v|version)=(?P<version>[0-9\.]+)/' 
    routing_loader: 
     default_format: json 
    view: 
     mime_types: 
      json: ['application/json;version=1.0'] 
    format_listener: 
     enabled: true 

控制檯調試:路由器post_user_status

| Route Name | post_user_status          | 
| Path   | /api/user/status.{_format}        | 
| Path Regex | #^/api/user/status(?:\.(?P<_format>json|xml|html))?$#s | 
| Host   | ANY              | 
| Host Regex |               | 
| Scheme  | ANY              | 
| Method  | POST             | 
| Requirements | _format: json|xml|html         | 
| Class  | Symfony\Component\Routing\Route       | 
| Defaults  | _controller: AcmeSomeBundle:Default:postStatus   | 
|    | _format: json           | 
| Options  | compiler_class: Symfony\Component\Routing\RouteCompiler | 
+--------------+---------------------------------------------------------+ 

我也試圖在沒有用routing.ymlcondition: "request.attributes.get('version') == '1.0'"

我在想什麼?

+0

你錯就錯在你的路由必須改變'前綴:api'到'前綴:/ api' –

+0

@MohammadZareMoghadam謝謝,但即使有這種變化,我仍然得到同樣的錯誤 – Tek

+0

它添加到'format_listener'部分: ' - {path:'^/api',優先級:['json'],fallback_format:json,prefer_extension:false}' –

回答

1

部分由於@Mohammad在評論中指出我正確的方向,我最終找到了答案。

在再次查看FOSRest versioning documentation時,說明應完全按照字面意思進行。

之一要求的國家

您必須配置可能的MIME類型爲所有受支持版本:

這意味着,因爲我只用了:

view: 
    mime_types: 
     json: ['application/json;version=1.0'] 

即使在發送的頭文件中請求了一個不存在的API版本,無論如何它都保持默認。

正確的配置是

view: 
    mime_types: 
     json: ['application/json','application/json;version=1.0'] 

與Mohhamad的提沿着media_type頭需要format_listener

format_listener: 
    enabled: true 
    rules: 
     - { path: '^/api', priorities: ['json'], fallback_format: json, prefer_extension: false } 

有了這一點,正確的路由到正確的API版本號。

如果版本號不正確或者沒有API版本標頭,它最後還會路由到404響應。