Installation
composer require dmykos/ip-store-bundle
Enable the bundle in config/bundles.php:
return [
// ...
Dmykos\IpStoreBundle\DmykosIpStoreBundle::class => ['all' => true],
];
Database Configuration
Create config/packages/ip_store.yaml:
dmykos_ip_store:
store_driver: dmykos_ip_store.database_store_driver
database:
table_name: ip_store
id_column_name: id
id_column_value: ip_address
key_column_name: count
Ensure the table exists (e.g., via a migration):
// src/Migration/VersionYYYYMMDDHHMMSS.php
public function up(Schema $schema)
{
$this->addSql('CREATE TABLE ip_store (
id INT AUTO_INCREMENT PRIMARY KEY,
ip_address VARCHAR(45) NOT NULL,
count INT DEFAULT 0
)');
}
First Use Case Add an IP via REST:
curl -X POST http://your-app.com/ip/add/192.168.1.1
Query its count:
curl http://your-app.com/ip/query/192.168.1.1
Storing IPs
/ip/add/{ip}) for quick integration.IpStore service:
use Dmykos\IpStoreBundle\Service\IpStore;
public function __construct(private IpStore $ipStore) {}
$this->ipStore->add('192.168.1.1');
Querying IPs
GET /ip/query/{ip} returns the count.$count = $this->ipStore->query('192.168.1.1');
Batch Operations
foreach ($ips as $ip) {
$this->ipStore->add($ip);
}
FilterVar::IP).Symfony\Contracts\Cache\CacheInterface).IpAddedEvent) for side effects.Table Schema Mismatch
id_column_name, id_column_value, and key_column_name match your table columns exactly.bin/console debug:container dmykos_ip_store.database_store_driver
IPv6 Handling
ip_address) uses VARCHAR(45) to accommodate full IPv6 addresses.REST Endpoint Quirks
/ip/add endpoint overwrites counts if the IP exists. Use increment logic if needed:
$this->ipStore->query('192.168.1.1'); // Get current count
$this->ipStore->add('192.168.1.1'); // Increment by 1
store_driver in ip_store.yaml matches the registered service name.bin/console doctrine:schema:validate
Custom Drivers
Dmykos\IpStoreBundle\Driver\StoreDriverInterface for non-database stores (e.g., Redis):
class RedisStoreDriver implements StoreDriverInterface {
public function add($ip) { /* ... */ }
public function query($ip) { /* ... */ }
}
services.yaml:
services:
dmykos_ip_store.redis_driver:
class: App\Driver\RedisStoreDriver
tags: ['dmykos_ip_store.driver']
Middleware
// src/EventListener/IpTracker.php
public function onKernelRequest(GetResponseEvent $event) {
$ip = $event->getRequest()->getClientIp();
$this->ipStore->add($ip);
}
Register in services.yaml:
services:
App\EventListener\IpTracker:
tags: [kernel.event_listener, kernel.request]
How can I help you explore Laravel packages today?