Symfony 5 Restful Api Bundle
- Supports XML and JSON response
- Error messages are collected under a single format.
- Language translation is applied to all error messages.
- Request body Transformer (JSON-XML) has been added.
- Normalizer has been added for form errors.
- Normalizer has been added for KnpPaginator.
Open a command console, enter your project directory and execute the following command to download the latest stable version of this bundle:
composer require appaydin/pd-api
This command requires you to have Composer installed globally, as explained in the installation chapter of the Composer documentation.
With Symfony 5, the package will be activated automatically. But if something goes wrong, you can install it manually.
Then, enable the bundle by adding it to the list of registered bundles
in the config/bundles.php
file of your project:
<?php
// config/bundles.php
return [
//...
Pd\ApiBundle\PdApiBundle::class => ['all' => true]
];
# config/packages/pd_api.yaml
pd_api:
zone: ["^/api"]
default_accept: json
default_groups: ['default']
allow_accept: ['json', 'xml']
# config/packages/security.yaml
security:
providers:
...
pdadmin_api:
entity:
class: App\Entity\User
property: phone
firewalls:
...
api:
pattern: ^/api
stateless: true
anonymous: true
provider: pdadmin_api
json_login:
check_path: /api/auth/login
failure_handler: lexik_jwt_authentication.handler.authentication_failure
guard:
authenticators:
- lexik_jwt_authentication.jwt_token_authenticator
access_control:
- { path: ^/api/auth, roles: IS_AUTHENTICATED_ANONYMOUSLY }
- { path: ^/api, roles: IS_AUTHENTICATED_FULLY }
# src/Controller/AuthorizationController.php
namespace App\Controller;
use Pd\ApiBundle\Controller\AbstractApiController;
use Pd\ApiBundle\Controller\LoginTrait;
class AuthorizationController extends AbstractApiController
{
use LoginTrait;
}
# config/routes.yaml
api:
resource: ../src/Controller
type: annotation
prefix: api
# src/Controller/ExampleApiController.php
use Pd\ApiBundle\Controller\AbstractApiController;
use Symfony\Component\Routing\Annotation\Route;
class ExampleApiController extends AbstractApiController
{
#[Route("/home", name:"api.home", methods: ["GET"])]
public function home() {
return ['test'];
}
}