ailove-dev/vk-api-helper-bundle
Installation
Add the bundle to your composer.json:
composer require ailove-dev/vk-api-helper-bundle
Ensure ailove-dev/vk-bundle (dev-master) is also installed as a dependency.
Bundle Registration
Add to config/bundles.php:
return [
// ...
Ailove\VKBundle\VKBundle::class => ['all' => true],
Ailove\VKApiHelperBundle\VKApiHelperBundle::class => ['all' => true],
];
Configuration
Extend your config/packages/ailove_vk.yaml (or equivalent) to include helper-specific settings:
ailove_vk:
api_version: '5.135'
helpers:
enabled: true
default_methods: ['getUserInfo', 'uploadPhoto']
First Use Case Inject the helper into a service/controller:
use Ailove\VKApiHelperBundle\Helper\VKHelper;
class MyController extends AbstractController
{
public function index(VKHelper $helper)
{
$userInfo = $helper->getUserInfo(123456789); // Extended method
return $this->json($userInfo);
}
}
Method Chaining Leverage extended methods for common VK API tasks:
$helper->uploadPhoto($filePath)
->getPhotoUrl()
->shareToWall($postId);
Batch Operations Use helper methods for bulk API calls (e.g., fetching multiple user profiles):
$helper->batchGetUsers([1, 2, 3, 4]);
Event-Driven Extensions Subscribe to VK API events via the helper’s event dispatcher:
$helper->on('photo.uploaded', function ($event) {
// Handle uploaded photo
});
Integration with Symfony Forms Bind VK API responses to form fields:
$form = $this->createFormBuilder()
->add('vkPhoto', EntityType::class, [
'class' => VKPhoto::class,
'query_builder' => function (VKHelper $helper) {
return $helper->getPhotosQueryBuilder();
}
])
->getForm();
$helper->getUserInfo(123456789, 3600); // Cache for 1 hour
try {
$helper->execute('messages.send', ['message' => 'Hello']);
} catch (\Ailove\VKApiHelperBundle\Exception\VKApiException $e) {
$this->addFlash('error', $e->getMessage());
}
config/packages/ailove_vk.yaml:
ailove_vk:
helpers:
custom_methods:
getUserWall: 'wall.get'
Dependency on Dev-Master
ailove-dev/vk-bundle:dev-master, which may introduce instability. Pin to a specific commit if possible:
composer require ailove-dev/vk-bundle@abc123
Method Signature Conflicts
users.get) may clash with helper extensions. Prefix custom methods:
$helper->vkHelper_getUserInfo($userId); // Explicit namespace
Rate Limiting
$helper = new RateLimitDecorator($originalHelper, 5); // 5 calls/sec
Deprecated API Methods
photos.getAll). Monitor VK API changes and update configurations.Enable Verbose Logging
Add to config/packages/monolog.yaml:
handlers:
vk_helper:
type: stream
path: '%kernel.logs_dir%/vk_helper.log'
level: debug
channels: ['vk_helper']
Then enable in VKHelper:
$helper->setDebug(true);
Inspect Raw Responses
Use the getLastResponse() method to debug API calls:
$helper->execute('users.get', ['user_id' => 1]);
$response = $helper->getLastResponse();
Custom Helper Methods Extend the helper by creating a service:
# config/services.yaml
services:
App\Service\CustomVKHelper:
decorates: 'ailove_vk.helper'
arguments: ['@.inner']
calls:
- [addMethod, ['myCustomMethod', [App\Service\CustomVKHelper::class, 'handleMyMethod']]]
Event Listeners Listen for helper events to modify responses:
// src/EventListener/VKHelperListener.php
class VKHelperListener
{
public function onResponse(VKResponseEvent $event)
{
$event->setResponse($this->sanitizeResponse($event->getResponse()));
}
}
Register in config/services.yaml:
services:
App\EventListener\VKHelperListener:
tags:
- { name: 'kernel.event_listener', event: 'vk_helper.response', method: 'onResponse' }
Configuration Validation Validate custom configurations in a compiler pass:
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
use Symfony\Component\DependencyInjection\ContainerBuilder;
class VKHelperCompilerPass implements CompilerPassInterface
{
public function process(ContainerBuilder $container)
{
$helpers = $container->getParameter('ailove_vk.helpers');
if (isset($helpers['custom_methods'])) {
foreach ($helpers['custom_methods'] as $method => $apiMethod) {
if (!method_exists($container->get('vk.api'), $apiMethod)) {
throw new \InvalidArgumentException("API method {$apiMethod} does not exist.");
}
}
}
}
}
How can I help you explore Laravel packages today?