Installation:
composer require arguv/crud-api-bundle:dev-master
Add to config/bundles.php (Symfony 4+) or AppKernel.php (Symfony 3):
Arguv\CrudApiBundle\CrudApiBundle::class => ['all' => true],
Routing:
Add to config/routes.yaml:
arguv_crud_api:
resource: '@CrudApiBundle/Controller/'
type: annotation
First Use Case:
src/Entity/Book.php).#[ORM\Entity] and define fields (e.g., name, description).php bin/console doctrine:schema:update --force
GET /arguv/list → List all records.POST /arguv/create → Create a new record (JSON payload).Entity-Centric Development:
#[ORM\Column], #[ORM\Id]).Book → /arguv/book/list).Request/Response Handling:
create/update (e.g., {"name": "Smith", "description": "..."}).[{"name": "Smith", "id": 1}]).SerializerInterface.Integration with Services:
#[Autowired]).kernel.event_listener).Pagination:
?page=1&limit=10) to list endpoints.CrudApiBundle/Controller/AbstractCrudController to support custom pagination logic.Validation:
Validator component to validate incoming data.use Symfony\Component\Validator\Constraints as Assert;
#[ORM\Entity]
class Book {
#[Assert\NotBlank]
#[ORM\Column]
private string $name;
}
Symfony Version Mismatch:
composer require symfony/framework-bundle:^3.4 if downgrading.Auto-Routing Overrides:
arguv_crud_api_book:
resource: '@CrudApiBundle/Controller/BookController'
type: annotation
prefix: /api/v1
Missing Documentation:
Soft Deletes:
delete endpoint performs hard deletes by default. For soft deletes:
AbstractCrudController and override deleteAction().preRemove).CORS Issues:
cors bundle:
# config/packages/nelmio_cors.yaml
nelmio_cors:
defaults:
allow_origin: ["*"]
allow_methods: ["GET", "POST", "PUT", "DELETE"]
Custom Controllers:
src/Controller/CustomBookController) and extending AbstractCrudController:
class CustomBookController extends AbstractCrudController {
protected static string $entityClass = Book::class;
// Override methods (e.g., createAction(), listAction())
}
Filtering/Sorting:
?name=Smith) or sorting (e.g., ?sort=-createdAt).listAction() in your custom controller to parse and apply filters:
$queryBuilder = $this->getEntityManager()
->getRepository(self::$entityClass)
->createQueryBuilder('e');
if ($name = $request->query->get('name')) {
$queryBuilder->andWhere('e.name LIKE :name')->setParameter('name', "%$name%");
}
Testing:
WebTestCase to test endpoints:
public function testCreateBook() {
$client = static::createClient();
$client->request('POST', '/arguv/book/create', [
'json' => ['name' => 'Test', 'description' => 'Test Book']
]);
$this->assertEquals(201, $client->getResponse()->getStatusCode());
}
Performance:
Extending with Events:
crud.api.pre_create) to inject logic:
// src/EventListener/CrudListener.php
class CrudListener {
public function onPreCreate(PreCreateEvent $event) {
$data = $event->getData();
$data['createdBy'] = $this->getUserId(); // Custom logic
$event->setData($data);
}
}
Register in services.yaml:
services:
App\EventListener\CrudListener:
tags:
- { name: kernel.event_listener, event: crud.api.pre_create }
Debugging:
# config/packages/dev/doctrine.yaml
doctrine:
dbal:
logging: true
profiling: true
How can I help you explore Laravel packages today?