Install Dependencies Run:
composer require voryx/restgeneratorbundle dev-master
Ensure FOSRestBundle, JMSSerializerBundle, and NelmioCorsBundle are also installed (required).
Register the Bundle
Add to config/bundles.php:
Voryx\RESTGeneratorBundle\VoryxRESTGeneratorBundle::class => ['all' => true],
Configure Required Bundles
Update config/packages/fos_rest.yaml:
framework:
csrf_protection: false # Disable for public APIs
fos_rest:
routing_loader:
default_format: json
view:
view_response_listener: force
Generate a REST Controller
Run the generator command for an entity (e.g., App\Entity\Post):
php bin/console voryx:rest:generate App\Entity\Post
This creates a controller with CRUD actions (GET, POST, PUT, DELETE) in src/Controller/.
Route the Controller
Ensure your routes are defined in config/routes.yaml or via annotations.
Generate a REST API for a User entity:
php bin/console voryx:rest:generate App\Entity\User
This auto-generates:
Test with:
curl -X GET http://your-app/api/users -H "Accept: application/json"
Entity-First Development
Define your Doctrine entity (e.g., Product):
// src/Entity/Product.php
#[ORM\Entity]
class Product {
#[ORM\Id, ORM\GeneratedValue]
private ?int $id = null;
#[ORM\Column]
private string $name;
}
Generate the REST layer immediately:
php bin/console voryx:rest:generate App\Entity\Product
Customizing Actions Override generated methods in the controller:
// src/Controller/ProductController.php
public function postAction(Request $request) {
$data = $this->get('request')->request->all();
// Custom validation/logic
return parent::postAction($request);
}
Nested Resources
For nested routes (e.g., /users/{id}/posts), extend the generator or manually define routes in routing.yml:
app_user_post:
path: /users/{user_id}/posts
defaults: { _controller: App\Controller\PostController::indexAction }
Pagination
Use FOSRestBundle's pagination (configure in fos_rest.yaml):
pagination:
max_per_page: 30
persistent_max_per_page: true
Filtering/Sorting
Leverage FOSRestBundle's query parameters:
curl http://your-app/api/users?filter[name]=John&sort[createdAt]=desc
LexikJWTAuthenticationBundle or FOSUserBundle for secured endpoints.
Example:
# config/packages/security.yaml
access_control:
- { path: ^/api/users, roles: ROLE_USER }
#[Assert\NotBlank]
private string $name;
Symfony Panther or Guzzle to test generated endpoints:
$client = static::createClient();
$response = $client->request('GET', '/api/users');
$this->assertResponseIsSuccessful();
CSRF Protection
csrf_protection globally may expose your API to CSRF attacks.fos_rest.view.disable_csrf_role to restrict CSRF bypass to specific roles:
fos_rest:
disable_csrf_role: ROLE_API_USER
Overwriting Generated Code
// src/Controller/ProductController.php
class ProductController extends GeneratedProductController {
public function customMethod() { ... }
}
CORS Misconfiguration
nelmio_cors:
paths:
'^/api/':
allow_origin: ['*']
allow_methods: ['GET', 'POST', 'PUT', 'DELETE']
allow_headers: ['Content-Type', 'Authorization']
Serialization Errors
config/packages/jms_serializer.yaml:
handlers:
DateTimeHandler: ~
Route Conflicts
routing.yml:
app_rest:
resource: "@VoryxRESTGeneratorBundle/Resources/config/routing.yml"
prefix: /api
Check Generated Code
Inspect the generated controller at src/Controller/ to verify logic.
Enable Debug Mode
Set APP_DEBUG=true in .env to see detailed errors.
Log Requests
Use Symfony’s profiler (/_profiler) or add a listener:
// src/EventListener/RequestLogger.php
public function onKernelRequest(GetResponseEvent $event) {
error_log($event->getRequest()->getContent());
}
Validate Entities Use Symfony’s validator component to debug entity issues:
$validator = $this->get('validator');
$errors = $validator->validate($entity);
Custom Actions Add new methods to the generated controller and route them manually:
# config/routes.yaml
app_product_export:
path: /api/products/export
methods: GET
defaults: { _controller: App\Controller\ProductController::exportAction }
Dynamic Routing
Use FOSRestBundle's dynamic routing to support custom formats:
fos_rest:
routing_loader:
default_format: json
include_format: true
Event Subscribers
Hook into lifecycle events (e.g., prePersist) via Doctrine listeners:
// src/EventListener/ProductListener.php
#[ORM\HasLifecycleCallbacks]
class Product {
#[ORM\PrePersist]
public function setCreatedAt() {
$this->createdAt = new \DateTime();
}
}
API Documentation
Integrate with NelmioApiDocBundle for Swagger/OpenAPI docs:
nelmio_api_doc:
routes:
path: /api/doc
How can I help you explore Laravel packages today?