symfony 3 和 FOSRest 包路由
symfony 3 and FOSRest Bundle Routing
我正在尝试使用 FOS REST Bundle 设置 symfony3 代码库,但我最终没有找到路由错误
AppBundle\Resources\config\api-routing.yml
check:
type: rest
resource: "@AppBundle/Controller/CheckController.php"
app\config\routing.yml
app:
type: rest
prefix: /
resource: '@AppBundle/Resources/config/api-routing.yml'
控制器
namespace AppBundle\Controller;
use FOS\RestBundle\Controller\FOSRestController;
use FOS\RestBundle\Controller\Annotations\Version;
use FOS\RestBundle\Controller\Annotations\RouteResource;
use Symfony\Component\HttpFoundation\Request;
/**
* @RouteResource("Check")
* @Version("v1")
*/
class CheckController extends FOSRestController
{
public function getAction(Request $request)
{
$data = "Ok";
return new JsonResponse($data);
}
}
http://127.0.0.1:8000/v1/app/check/ return no route
我想集成 rest api 路由的版本控制。你能分享错误是什么吗?我该如何纠正?
fos_rest:
body_listener: true
versioning:
enabled: true
resolvers:
query:
enabled: true
parameter_name: version
format_listener:
rules:
- { path: '^/', priorities: ['json'], fallback_format: json, prefer_extension: false }
param_fetcher_listener: true
view:
view_response_listener: 'force'
formats:
json: true
这是工作示例。
app\config\routng.yml
app:
type: rest
resource: "@AppBundle/Resources/config/routing.yml"
prefix: /api
AppBundle\Resources\config\routing.yml
rest_my:
type: rest
prefix: /v1
resource: "@AppBundle/Controller/MyRestController.php"
name_prefix: api_1_
活动路线将是:
http://127.0.0.1:8000/api/v1/<MyRestControllerAction>
或第二个控制器
AppBundle\Resources\config\routing.yml
rest_user:
type: rest
prefix: /v1/user
resource: "@AppBundle/Controller/UserRestController.php"
name_prefix: api_1_
活动路线将是:
http://127.0.0.1:8000/api/v1/user/<UserRestControllerAction>
希望能帮到你...干杯。
更新#1:
这是我完整的 fos_rest 配置 json 格式到 config.yml 文件:
fos_rest:
routing_loader:
default_format: json
include_format: false
body_converter:
enabled: true
body_listener:
decoders:
json: fos_rest.decoder.json
format_listener:
enabled: true
rules:
- { path: '^/api/v1/', priorities: ['json'], fallback_format: json, prefer_extension: true}
- { path: '^/', priorities: ['json', 'html'], fallback_format: 'html' }
serializer:
serialize_null: true
view:
view_response_listener: 'force'
templating_formats:
html: false
param_fetcher_listener: 'force'
如果您需要更多,那么您有 fos_rest 的完整文档
http://symfony.com/doc/current/bundles/FOSRestBundle/configuration-reference.html
我正在尝试使用 FOS REST Bundle 设置 symfony3 代码库,但我最终没有找到路由错误
AppBundle\Resources\config\api-routing.yml
check:
type: rest
resource: "@AppBundle/Controller/CheckController.php"
app\config\routing.yml
app:
type: rest
prefix: /
resource: '@AppBundle/Resources/config/api-routing.yml'
控制器
namespace AppBundle\Controller;
use FOS\RestBundle\Controller\FOSRestController;
use FOS\RestBundle\Controller\Annotations\Version;
use FOS\RestBundle\Controller\Annotations\RouteResource;
use Symfony\Component\HttpFoundation\Request;
/**
* @RouteResource("Check")
* @Version("v1")
*/
class CheckController extends FOSRestController
{
public function getAction(Request $request)
{
$data = "Ok";
return new JsonResponse($data);
}
}
http://127.0.0.1:8000/v1/app/check/ return no route
我想集成 rest api 路由的版本控制。你能分享错误是什么吗?我该如何纠正?
fos_rest:
body_listener: true
versioning:
enabled: true
resolvers:
query:
enabled: true
parameter_name: version
format_listener:
rules:
- { path: '^/', priorities: ['json'], fallback_format: json, prefer_extension: false }
param_fetcher_listener: true
view:
view_response_listener: 'force'
formats:
json: true
这是工作示例。
app\config\routng.yml
app:
type: rest
resource: "@AppBundle/Resources/config/routing.yml"
prefix: /api
AppBundle\Resources\config\routing.yml
rest_my:
type: rest
prefix: /v1
resource: "@AppBundle/Controller/MyRestController.php"
name_prefix: api_1_
活动路线将是:
http://127.0.0.1:8000/api/v1/<MyRestControllerAction>
或第二个控制器
AppBundle\Resources\config\routing.yml
rest_user:
type: rest
prefix: /v1/user
resource: "@AppBundle/Controller/UserRestController.php"
name_prefix: api_1_
活动路线将是:
http://127.0.0.1:8000/api/v1/user/<UserRestControllerAction>
希望能帮到你...干杯。
更新#1: 这是我完整的 fos_rest 配置 json 格式到 config.yml 文件:
fos_rest:
routing_loader:
default_format: json
include_format: false
body_converter:
enabled: true
body_listener:
decoders:
json: fos_rest.decoder.json
format_listener:
enabled: true
rules:
- { path: '^/api/v1/', priorities: ['json'], fallback_format: json, prefer_extension: true}
- { path: '^/', priorities: ['json', 'html'], fallback_format: 'html' }
serializer:
serialize_null: true
view:
view_response_listener: 'force'
templating_formats:
html: false
param_fetcher_listener: 'force'
如果您需要更多,那么您有 fos_rest 的完整文档 http://symfony.com/doc/current/bundles/FOSRestBundle/configuration-reference.html