Installation:
composer require antwebes/social-rest-bundle
Enable the bundle in app/AppKernel.php:
new Ant\SocialRestBundle\AntSocialRestBundle(),
Configuration:
Ensure FOSRestBundle is installed (required for error handling and API responses).
Configure error codes in app/config/codes.yml:
profile:
user:
exist:
message: "User already exists."
code: 409
First Use Case: Trigger a profile visit via API:
POST /api/users/{user_id}/visits
Or for authenticated users:
POST /api/users/{id}/profiles/visits
Profile Management:
Ant\SocialRestBundle\Controller\ProfileController for custom profile logic.AntSocialRestEvents) to hook into lifecycle methods (e.g., PROFILE_SHOW_COMPLETED).Event-Driven Extensions:
ProfileResponseEvent):
// src/Acme/UserBundle/Listener/ProfileListener.php
class ProfileListener implements EventSubscriberInterface {
public static function getSubscribedEvents() {
return [
AntSocialRestEvents::PROFILE_SHOW_COMPLETED => 'onProfileShowCompleted',
];
}
public function onProfileShowCompleted(ProfileResponseEvent $event) {
$event->getResponse()->headers->set('X-Custom-Header', 'value');
}
}
services.yml:
services:
acme.profile_listener:
class: Acme\UserBundle\Listener\ProfileListener
tags:
- { name: kernel.event_subscriber }
Error Handling:
serviceError() in controllers to return standardized errors:
return $this->serviceError('profile.user.exist', 409);
api.servicio.code_loader is configured to load codes.yml.Profile Visits:
/api/users/{id}/visits (increment counter).POST /api/users/{id}/profiles/visits) and anonymous visits.ProfileController::editAction).COMPLETED events to modify responses (e.g., add cache headers):
$event->getResponse()->setSharedMaxAge(3600); // Cache for 1 hour
routing.yml:
acme_profile:
resource: "@AntSocialRestBundle/Resources/config/routing.yml"
prefix: /api
Event Naming:
PROFILE_SHOW_COMPLETED vs profile_show_completed).AntSocialRestEvents class before subscribing.Error Codes:
codes.yml entries will throw UndefinedIndexException.409) match Symfony’s valid statuses.Deprecated Features:
visitDate as a primary key (deprecated in v0.1.1; use id instead).FOSRestBundle Dependency:
FOSRestBundle, error responses (e.g., ErrorResponse::createResponse) will fail.debug:event-dispatcher to list active events:
php bin/console debug:event-dispatcher
public function onProfileShowCompleted(ProfileResponseEvent $event) {
error_log(print_r($event->getResponse()->getContent(), true));
}
Custom Entities:
Visit entity (e.g., app/Resources/AntSocialRestBundle/Entity/Visit.php) to add fields like ip_address:
/**
* @ORM\Column(type="string", length=45)
*/
private $ipAddress;
Dynamic Redirects:
COMPLETED events to change redirects:
public function onProfileUpdateCompleted(ProfileResponseEvent $event) {
$event->setResponse(new RedirectResponse('/custom-path'));
}
Validation:
INITIALIZE events:
public function onProfileInitialize(ProfileEvent $event) {
$event->getForm()->add('custom_field', 'text', [
'constraints' => [new NotBlank()]
]);
}
Performance:
codes.yml loading in code_loader service for high-traffic APIs:
services:
api.servicio.code_loader:
class: Ant\SocialRestBundle\Service\CodeLoader
calls:
- [setCache, ['%kernel.cache_dir%/codes_cache']]
How can I help you explore Laravel packages today?