Installation
Add the package to your composer.json under require:
"ais/gedungbundle": "dev-master"
Run composer update to install dependencies.
Register the Bundle
In AppKernel.php, add the bundle to the $bundles array:
new Ais\GedungBundle\AisGedungBundle(),
Ensure dependencies (NelmioApiDocBundle, FOSRestBundle, JMSSerializerBundle) are also registered.
Configure Routing
Import the bundle’s routes in app/config/routing.yml:
ais_gedungs:
type: rest
prefix: /api
resource: "@AisGedungBundle/Resources/config/routes.yml"
Access API Documentation
Visit /api/doc (e.g., http://localhost/web/app_dev.php/api/doc) to explore the API endpoints via NelmioApiDoc.
POST /api/gedungs to create, GET /api/gedungs to list, and GET /api/gedungs/{id} to fetch a single entity.FOSRestBundle for standardized JSON responses and JMSSerializerBundle for data transformation.Entity Management
/api/gedungs with required fields (e.g., name, address).
curl -X POST -H "Content-Type: application/json" -d '{"name":"Gedung A", "address":"Jl. Raya 1"}' http://localhost/api/gedungs
GET /api/gedungs or GET /api/gedungs/{id}.PUT/PATCH or DELETE with the respective endpoint.Validation
Validation errors in responses (HTTP 400).API Documentation
@SWG\ annotations in controllers.AisGedungBundle/Resources/config/doctrine/ if needed.GedungController in your app with @Route annotations.LiipFunctionalTestBundle (dev dependency) for API tests. Example:
$client = static::createClient();
$client->request('GET', '/api/gedungs');
$this->assertEquals(200, $client->getResponse()->getStatusCode());
Symfony Version Mismatch
FOSRestBundle@dev or NelmioApiDocBundle@dev. Pin versions in composer.json:
"friendsofsymfony/rest-bundle": "~2.0",
"nelmio/api-doc-bundle": "~2.12"
Missing Dependencies
JMSSerializerBundle and FOSRestBundle are properly configured. Missing them breaks API serialization/routing.Route Overrides
routing.yml extends the bundle’s routes:
ais_gedungs:
resource: "@AisGedungBundle/Resources/config/routes.yml"
type: rest
prefix: /api
/app_dev.php/_profiler) for detailed errors.php app/console doctrine:schema:validate
php app/console cache:clear
Custom Fields
Gedung entity by creating a subclass in your bundle:
// src/Acme/MyBundle/Entity/GedungExtension.php
namespace Acme\MyBundle\Entity;
use Ais\GedungBundle\Entity\Gedung as BaseGedung;
class GedungExtension extends BaseGedung {
// Add custom properties/methods
}
orm.xml or annotations to include the extension.Event Listeners
prePersist) to add logic:
// src/Acme/MyBundle/EventListener/GedungListener.php
namespace Acme\MyBundle\EventListener;
use Doctrine\ORM\Event\LifecycleEventArgs;
class GedungListener {
public function prePersist(LifecycleEventArgs $args) {
$gedung = $args->getEntity();
// Add custom logic
}
}
services.yml:
services:
acme.my_bundle.listener.gedung:
class: Acme\MyBundle\EventListener\GedungListener
tags:
- { name: doctrine.event_listener, event: prePersist }
API Extensions
GedungController:
// src/Acme/MyBundle/Controller/GedungController.php
namespace Acme\MyBundle\Controller;
use Ais\GedungBundle\Controller\GedungController as BaseController;
class GedungController extends BaseController {
public function customAction() {
return $this->handleView($this->get('gedung.manager')->customLogic());
}
}
routing.yml to include the new route.config.yml:
nelmio_api_doc:
areas: # Use multiple areas
path_patterns: [ ^/api ]
documentation:
info:
title: "My Gedung API"
description: "API for managing buildings"
config.yml:
fos_rest:
format_listener:
rules:
- { path: ^/api, priorities: [ json ], fallback_format: json }
How can I help you explore Laravel packages today?