adrotec/webapi-bundle
Unmaintained Symfony 2 bundle for building web APIs with breeze.server.php. Registers an adrotec_webapi service to map request resources to entities and handle API requests, while overriding some JMSSerializerBundle behaviors (naming, lazy loading, etc.).
Installation:
composer require adrotec/webapi-bundle:dev-master symfony/validator:dev-master
Ensure you’re using Symfony 2.x (this bundle is unmaintained and tied to it).
Enable the Bundle:
Add to AppKernel.php before JMSSerializerBundle (order matters):
new Adrotec\WebApiBundle\AdrotecWebApiBundle(),
new JMS\SerializerBundle\JMSSerializerBundle(),
Configure Routing:
Define a route with {resource} parameter (e.g., /api/users):
# src/Acme/Bundle/Resources/config/routing.yml
api:
path: /api/{resource}
defaults: { _controller: AcmeBundle:Api:api }
Create a Resource Class:
Define a class (e.g., User) with properties and validation rules (e.g., @Assert\NotBlank).
First API Call:
Access /api/users with HTTP methods (GET, POST, etc.) to test CRUD operations. The bundle auto-generates JSON responses for Breeze.js or similar clients.
Resource-Based Routing:
{resource} in routes to dynamically map to classes (e.g., User, Product)./api/users → User class, /api/products → Product class.Controller-Free API:
ApiController handles all CRUD logic. No need to manually define actions like index(), store(), etc.Validation Integration:
Assert constraints (e.g., @Assert\Email) in your resource classes. The bundle validates requests automatically.Serialization:
JMSSerializerBundle for JSON output. Customize serialization via annotations (e.g., @SerializedName).Breeze.js Compatibility:
{ data: [...], meta: {...} }).Custom Responses:
Override AdrotecWebApiBundle:Api:api in your routing to extend behavior (e.g., add authentication middleware).
api_custom:
path: /api/{resource}
defaults: { _controller: AcmeBundle:Api:customApi }
Nested Resources:
Use dot notation in routes (e.g., /api/users/{user}/posts) and define relationships in your resource classes.
Pagination:
The bundle supports pagination via query parameters (e.g., ?page=1&limit=10). Customize in config.yml:
adrotec_webapi:
pagination:
default_limit: 20
Authentication:
Integrate with Symfony’s security system by extending the ApiController or using event listeners (e.g., kernel.request).
Symfony 2.x Only:
JMSSerializerBundle Conflicts:
JMSSerializerBundle behaviors (e.g., naming strategy). If you need custom serialization, extend the bundle’s services or use a separate serializer.Unmaintained:
Validation Quirks:
symfony/validator is installed (required for Breeze validation). Custom validators may need manual registration.Route Overrides:
api controller, ensure you replicate all bundle logic (e.g., resource resolution, HTTP method handling).404 Errors:
Verify the {resource} parameter matches a valid class name (case-sensitive). Check routing cache (app/cache).
Validation Failures: Enable Symfony’s profiler to inspect validation errors. Example response:
{
"errors": {
"email": ["This value should not be blank."]
}
}
Serialization Issues:
Clear the cache (php app/console cache:clear) after adding/removing @SerializedName annotations.
Custom Response Format:
Extend the bundle’s ResponseBuilder service to modify JSON structure:
# config.yml
adrotec_webapi:
response_builder: AcmeBundle\Service\CustomResponseBuilder
Add Middleware:
Use Symfony’s event system (e.g., kernel.request) to inject logic before API processing.
Custom Actions:
Create a separate controller extending ApiController and override methods like handleGet() or handlePost().
Database Abstraction:
The bundle assumes Doctrine ORM. For custom data sources, override the ResourceManager service.
Testing:
Use PHPUnit with Symfony’s WebTestCase to test API endpoints. Example:
$client->request('GET', '/api/users');
$this->assertJson($client->getResponse()->getContent());
Documentation: Refer to breeze.server.php for client-side integration details.
Fallback for Laravel: For Laravel projects, consider alternatives like:
laravel/api-resources (for API resources).fideloper/proxy + custom controllers (for Breeze.js compatibility).How can I help you explore Laravel packages today?