Installation Add the package via Composer:
composer require dbp/api-base-bundle
Enable the bundle in config/bundles.php (Symfony):
return [
// ...
Dbp\Relay\ApiBaseBundle\DbpApiBaseBundle::class => ['all' => true],
];
Configuration Publish the default config:
php bin/console dbp:api-base:install
Review and customize config/packages/dbp_api_base.yaml (Symfony) or config/api-base.php (Laravel).
First Use Case Generate a basic API endpoint:
php artisan make:api-controller UserController --resource
Extend the base controller (Dbp\Relay\ApiBaseBundle\Controller\ApiController) to leverage built-in features like:
DTO-Driven Development
Use the @ApiResource annotation (Symfony) or ApiResource trait (Laravel) to define API endpoints:
use Dbp\Relay\ApiBaseBundle\Annotation\ApiResource;
#[ApiResource(
collectionOperations: ['get', 'post'],
itemOperations: ['get', 'put', 'delete']
)]
class UserController extends ApiController
{
// ...
}
#[ApiResource] trait or ApiResource facade.Validation & Serialization
Leverage built-in validation via YAML/JSON schemas (e.g., config/api-base/validation/user.yaml):
User:
properties:
name:
type: string
minLength: 2
Pagination & Filtering
Use query parameters for pagination (?page=1&limit=10) and filtering (?name=John).
Override configurePagination() in controllers for custom logic.
Event-Driven Extensions
Subscribe to events like ApiRequestEvent or ApiResponseEvent:
// Symfony
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
class MySubscriber implements EventSubscriberInterface
{
public static function getSubscribedEvents()
{
return [
'api.request' => 'onApiRequest',
];
}
}
Laravel-Specific:
ApiBaseServiceProvider to bind interfaces to implementations.ApiBaseService for custom logic (e.g., app/Extensions/CustomApiService.php).ApiBaseMiddleware for global request/response modifications.Symfony-Specific:
api_base.twig templates for custom responses.Serializer for complex data transformations.Shared:
ApiTestCase (Symfony) or ApiTestTrait (Laravel) for automated tests.#[ApiResource] annotations.Configuration Overrides
config/api-base.php (Laravel) or config/packages/dbp_api_base.yaml (Symfony) to explicitly override values. Example:
'validation' => [
'strict' => false, // Disable strict validation
],
DTO Mapping Quirks
@SerializedName (Symfony) or $fillable (Laravel) to define mappings:
#[SerializedName('user_name')]
private string $name;
Caching Headers
public function configureResponseHeaders(): array
{
return [
'Cache-Control' => 'no-cache, no-store, must-revalidate',
];
}
Event Dispatching
'api.request' => ['onApiRequest', 255] // Highest priority
Enable Verbose Logging:
php artisan api-base:debug --verbose
(Laravel) or adjust Symfony’s monolog config.
Validate Annotations:
Use php artisan api-base:validate (Laravel) or Symfony’s debug:container to check for misconfigured annotations.
API Response Inspection: Enable JSON pretty-printing in config:
api_base:
response:
json_pretty_print: true
Custom Validators Create a validator class and bind it in config:
api_base:
validation:
custom_validators:
- App\Validator\CustomUserValidator
Middleware Stack Add custom middleware to the API stack (Laravel):
$middleware->apiBaseMiddleware(function ($middleware) {
$middleware->append(\App\Http\Middleware\CustomApiMiddleware::class);
});
Response Transformers
Override transformResponse() in controllers or create a global transformer:
use Dbp\Relay\ApiBaseBundle\Transformer\TransformerInterface;
class CustomTransformer implements TransformerInterface
{
public function transform($data, string $format, array $context = [])
{
// Custom logic
}
}
Register it in config:
api_base:
transformer:
default: App\Transformer\CustomTransformer
Error Handling
Extend ApiException or create custom exception handlers:
use Dbp\Relay\ApiBaseBundle\Exception\ApiException;
throw new ApiException('Custom error', 403, [], 'custom.error.code');
Map errors in config/api-base/errors.yaml.
How can I help you explore Laravel packages today?