Collections
Introduction
Collections are standard Phalcon Collections enriched with Access Control List and advanced Endpoints configuration. Collections can be used to register default endpoints, for more complex implementations look at Resources.
use Prest\Api;
use Acme\Acl\Roles;
use Prest\Api\Endpoint;
use Prest\Api\Collection;
use Acme\Controller\StatusController;
$api = new Api();
$api->collection(Collection::factory('/v1')
->name('Status')
->handler(StatusController::class)
->allow(Roles::USER)
->endpoint(Endpoint::get('/ping', 'pong'))
->endpoint(Endpoint::get('/status', 'status'))
);
This example provides you with a GET
endpoints with /v1/ping
and /v1/status
as path. The pong
and status
methods on StatusController
will be used to handle these endpoints.
Extend
Extend from StatusCollection
from Prest\Api\Collection
:
namespace Acme\Collection;
use Acme\Acl\Roles;
use Prest\Api\Endpoint;
use Prest\Api\Collection;
use Acme\Controller\StatusController;
class StatusCollection extends Collection
{
protected function initialize()
{
$this
->name('Status')
->handler(StatusController::class)
->allow(Roles::USER)
->endpoint(Endpoint::get('/ping', 'pong'))
->endpoint(Endpoint::get('/status', 'status'));
}
}
Register collection on Api
:
$api = new \Prest\Api();
$api->collection(new \Acme\Collection\StatusCollection('/v1'));