composer require alks/http-extra-bundle
config/bundles.php (Symfony 4+) or AppKernel.php (Symfony 3):
Alks\HttpExtraBundle\HttpExtraBundle::class => ['all' => true],
config/packages/framework.yaml:
framework:
annotations:
cache: null # Disable annotation cache for development
Create a controller with @RequestParam to bind a query parameter to a method argument:
use Alks\HttpExtraBundle\Annotation as Http;
class UserController extends AbstractController
{
/**
* @Route("/user/{id}", methods={"GET"})
* @Http\RequestParam(name="id", bindTo="userId")
*/
public function show(int $userId)
{
return $this->json(['id' => $userId]);
}
}
Request: GET /user/123?id=456 → $userId becomes 456.
Use @RequestBody to deserialize JSON/XML into a DTO:
use Alks\HttpExtraBundle\Annotation as Http;
class PostController extends AbstractController
{
/**
* @Route("/posts", methods={"POST"})
* @Http\RequestBody(type="json", bindTo="postDto")
*/
public function create(PostDto $postDto)
{
// $postDto is automatically populated from request body
return $this->json(['success' => true]);
}
}
Request Body:
{"title": "Hello", "content": "World"}
Resolve Doctrine entities from query params:
/**
* @Route("/user/{id}", methods={"GET"})
* @Http\RequestParam(name="id", bindTo="user", repository="AppBundle\Repository\UserRepository")
*/
public function show(User $user)
{
return $this->json($user);
}
Add headers or context to responses:
/**
* @Route("/data", methods={"GET"})
* @Http\Response(type="json", context={"groups": {"api"}})
*/
public function getData()
{
return new Post(); // Automatically serialized with API groups
}
Mix @RequestParam, @RequestBody, and @RequestData:
/**
* @Route("/update", methods={"PUT"})
* @Http\RequestParam(name="id", bindTo="id")
* @Http\RequestBody(type="json", bindTo="data")
*/
public function update(int $id, array $data)
{
// $id from query, $data from body
}
Leverage Symfony’s validator with DTOs:
use Symfony\Component\Validator\Constraints as Assert;
class PostDto
{
/**
* @Assert\NotBlank()
*/
public $title;
}
The bundle auto-validates and throws HttpException on failure.
Annotation Caching:
cache: null) during development to avoid stale annotations.php bin/console cache:clear) after adding new annotations.Doctrine Entity Resolution:
repository attribute to point to a valid Doctrine repository class.HttpException if the repository doesn’t exist or the entity isn’t found.defaultRepository in config to avoid repetition:
alks_http_extra:
default_repository: AppBundle\Repository
Content Negotiation:
willdurand/negotiation. Ensure Accept headers are set for non-default formats (e.g., XML).@Http\Response(type="json").Symfony 4+ Compatibility:
symfony/serializer and symfony/expression-language as dev dependencies for compatibility.Validation Errors:
HttpException with status 400. Customize error responses via:
alks_http_extra:
validation_error_handler: AppBundle\EventListener\ValidationErrorListener
Enable Debug Mode:
APP_DEBUG=1 to see detailed HttpException messages for binding/validation failures.Check Resolver Logs:
// config/packages/dev/debug.yaml
framework:
profiler: { only_exceptions: false }
Override Default Behavior:
http_extra.request_body_resolver) to customize deserialization.Custom Resolvers:
Alks\HttpExtraBundle\Resolver\ResolverInterface for custom argument binding logic.Response Transformers:
Alks\HttpExtraBundle\Response\Transformer\TransformerInterface to modify response serialization.Configuration:
config/packages/alks_http_extra.yaml:
alks_http_extra:
default_format: json # Override default response format
validation_groups: [Default, Api] # Global validation groups
validation: false in the annotation:
@Http\RequestBody(validation=false)
How can I help you explore Laravel packages today?