This Symfony bundle aims to provide simple serialization group recognize from query string (with basic security check if you need it).
composer require bartlomiejbeta/api-scope-bundle
AppKernel:public function registerBundles()
{
$bundles = array(
// ...
new BartB\APIScopeBundle\APIScopeBundle(),
);
}
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)
/**
* @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"]}
/**
* @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"]}
How can I help you explore Laravel packages today?