1. Go to this page and download the library: Download roadiz/abstract-api-theme library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
roadiz / abstract-api-theme example snippets
# AppKernel.php
/**
* {@inheritdoc}
*/
public function register(\Pimple\Container $container)
{
parent::register($container);
/*
* Add your own service providers.
*/
$container->register(new \Themes\AbstractApiTheme\Services\AbstractApiServiceProvider());
}
declare(strict_types=1);
namespace Themes\MyApiTheme;
use Symfony\Component\HttpFoundation\RequestMatcher;
use Pimple\Container;
use Themes\AbstractApiTheme\AbstractApiThemeTrait;
class MyApiThemeApp extends FrontendController
{
use AbstractApiThemeTrait;
protected static $themeName = 'My API theme';
protected static $themeAuthor = 'REZO ZERO';
protected static $themeCopyright = 'REZO ZERO';
protected static $themeDir = 'MyApiTheme';
protected static $backendTheme = false;
public static $priority = 10;
/**
* @inheritDoc
*/
public static function addDefaultFirewallEntry(Container $container)
{
/*
* API MUST be the first request matcher
*/
$requestMatcher = new RequestMatcher(
'^'.preg_quote($container['api.prefix']).'/'.preg_quote($container['api.version'])
);
$container['accessMap']->add(
$requestMatcher,
[$container['api.base_role']]
);
/*
* Add default API firewall entry.
*/
$container['firewallMap']->add(
$requestMatcher, // launch firewall rules for any request within /api/1.0 path
[$container['api.firewall_listener']],
$container['api.exception_listener'] // do not forget to add exception listener to enforce accessMap rules
);
/*
* OR add OAuth2 API firewall entry.
*/
// $container['firewallMap']->add(
// $requestMatcher, // launch firewall rules for any request within /api/1.0 path
// [$container['api.oauth2_firewall_listener']],
// $container['api.exception_listener'] // do not forget to add exception listener to enforce accessMap rules
// );
// Do not forget to register default frontend entries
// AFTER API not to lose preview feature
parent::addDefaultFirewallEntry($container);
}
}
/*
* Enable grant types
*/
$container->extend(AuthorizationServer::class, function (AuthorizationServer $server, Container $c) {
// Enable the client credentials grant on the server
$server->enableGrantType(
new \League\OAuth2\Server\Grant\ClientCredentialsGrant(),
new \DateInterval('PT1H') // access tokens will expire after 1 hour
);
// Enable the authorization grant on the server
$authCodeGrant = new \League\OAuth2\Server\Grant\AuthCodeGrant(
$c[AuthCodeRepositoryInterface::class],
$c[RefreshTokenRepositoryInterface::class],
new \DateInterval('PT10M') // authorization_codes will expire after 10 min
);
$server->enableGrantType(
$authCodeGrant,
new \DateInterval('PT3H') // access tokens will expire after 3 hours
);
return $server;
});