bartlomiejbeta/api-scope-bundle

简单的API查询字符串范围识别器

安装次数: 7,341

依赖项: 0

建议者: 0

安全性: 0

星星: 0

关注者: 1

分支: 1

开放问题: 1

类型:symfony-bundle

v3.0.1 2018-11-16 13:42 UTC

This package is auto-updated.

Last update: 2024-09-20 22:43:20 UTC


README

此Symfony扩展旨在提供简单的从查询字符串中识别序列化组的功能(如果需要,可进行基本的安全性检查)。

Build Status

安装

1. 使用composer安装
composer require bartlomiejbeta/api-scope-bundle
2. 在AppKernel中注册bundle
public function registerBundles()
{
    $bundles = array(
        // ...
		new BartB\APIScopeBundle\APIScopeBundle(),
    );
}
3. 在config.yml中进行配置
api_scope:
    scopes:
        api.get_item: #route name
            always_included: #will be always included to scopes bag
                - 'first_always_included_group'
                - 'second_always_included_group'
            supported_key_map:
                external1: { internal_name: 'scope.internal_name1'} #if `external1` will be in the query string than `scope.internal_name1` will be in the scopes bag
                external2:
                    internal_name: 'scope.internal_name2'
                    security: 'can-add-external2-scope' # security voter (check symfony security voter) attribution name (to check if scope can be applied)

使用方法

1. 简单

/**
* @ScopeConverter()
* @Rest\Route("/api/item", name="api.get_item")
*/
public function getCarCollection(Request $request, ScopeCollection $scopeCollection): Response
{
	$view = $this->view($scopeCollection, Response::HTTP_OK);
	
	$scopesFromQueryString = $scopeCollection->getScopes()
	
	return $this->handleView($view);
}
示例请求
.../api/item?with[]=external1
示例响应
{"scopes":["first_always_included_group","second_always_included_group","scope.internal_name1"]}

2. 已配置

/**
* @ScopeConverter(value="scopes",queryString="scope")
* @Rest\Route("/api/item", name="api.get_item")
*/
public function getCarCollection(Request $request, ScopeCollection $scopes): Response
{
	$view = $this->view($scope,Response::HTTP_OK);
		
	scopesFromQueryString = $scopeCollection->getScopes()
		
	return $this->handleView($view);
}
示例请求
.../api/item?scope[]=external1&scope[]=external2
示例响应
{"scopes":["first_always_included_group","second_always_included_group","scope.internal_name1","scope.internal_name2"]}