dmykos / ip-store-bundle
IP 存储包
2.0.1
2021-01-21 09:44 UTC
Requires
- php: ^7.1.3
- ext-json: *
- ext-pdo: *
- doctrine/doctrine-bundle: ^1.12
- doctrine/orm: ^2.7
- symfony/framework-bundle: ^3.4 || ^4.4
- symfony/validator: ^3.4 || ^4.4
Requires (Dev)
- symfony/browser-kit: ^3.4 || ^4.4
- symfony/phpunit-bridge: ^3.4 || ^4.4
This package is not auto-updated.
Last update: 2024-09-27 03:37:09 UTC
README
IpStoreBundle 用于存储和查询 IP 地址(IPv4 和 IPv6)。
应用程序设计为支持不同的存储驱动器。支持的存储驱动器包括
- 数据库存储 – 此驱动程序使用 PDO 运行,并能够将数据存储在定义的表中定义的行中。
应用程序可以通过不同的接口调用,因此可以集成到不同的系统中
支持的接口
- 基于 json 的 REST 接口
使用以下命令安装包
composer require dmykos/ip-store-bundle
如果您 未 使用 Symfony Flex,您还需要在您的 bundles.php 文件中启用 Dmykos\IpStoreBundle\DmykosIpStoreBundle。
使用方法
添加 IP 地址
example.com/ip/add/255.255.255.0
显示添加 IP 地址的次数
example.com/ip/query/255.255.255.0
配置
要使用数据库存储使用此包,您应该使用 Doctrine 配置数据库连接,并通过创建新的 config/packages/ip_store.yaml 文件直接配置 IpStoreBundle。示例值如下
# config/packages/ip_store.yaml dmykos_ip_store: store_driver: dmykos_ip_store.database_store_driver database: table_name: stores id_column_name: store_name id_column_value: ip key_column_name: store_value
上述表中指定的表名、id_column_name 和 key_column_name 应已在数据库中存在。
示例
// src/Entity/Stores.php namespace App\Entity; use Doctrine\ORM\Mapping as ORM; /** * @ORM\Entity(repositoryClass="App\Repository\StoresRepository") */ class Stores { /** * @ORM\Id() * @ORM\GeneratedValue() * @ORM\Column(type="integer") */ private $id; /** * @ORM\Column(type="string", unique=true, length=255) */ private $store_name; /** * @ORM\Column(type="text", length=65535, nullable=true) */ private $store_value; public function getId(): ?int { return $this->id; } public function getStoreName(): ?string { return $this->store_name; } public function setStoreName(string $store_name): self { $this->store_name = $store_name; return $this; } public function getStoreValue(): ?string { return $this->store_value; } public function setStoreValue(string $store_value): self { $this->store_value = $store_value; return $this; } }
还要创建新的 config/routes/ip_store.yaml 文件,以配置 API 控制器路由
_ip_store: resource: '@DmykosIpStoreBundle/Resources/config/routes.xml' prefix: '/ip'
扩展 IpStoreBundle
您可以使用自己的 StoreDriver 实现,而不是使用 DatabaseStoreDriver。
# config/packages/ip_store.yaml dmykos_ip_store: store_driver: App\Service\DummyStoreDriver
//src/Service/DummyStoreDriver.php namespace App\Service; use Dmykos\IpStoreBundle\Entity\IpModel; use Dmykos\IpStoreBundle\StoreDriverInterface; class DummyStoreDriver implements StoreDriverInterface { public function add( IpModel $ipModel ): int { return 1000; } public function query( IpModel $ipModel ): int { return 100; } }