setono /client-bundle
将客户端库集成到您的 Symfony 应用程序中
v1.0.0-beta.4
2024-05-21 09:02 UTC
Requires
- php: >=8.1
- doctrine/dbal: ^2.13 || ^3.0 || ^4.0
- doctrine/doctrine-bundle: ^2.7
- doctrine/orm: ^2.0 || ^3.0
- doctrine/persistence: ^2.5 || ^3.0
- psr/event-dispatcher: ^1.0
- psr/log: ^1.1 || ^2.0 || ^3.0
- setono/client: ^1.1.1
- setono/doctrine-orm-trait: ^1.1
- symfony/config: ^6.4 || ^7.0
- symfony/dependency-injection: ^6.4 || ^7.0
- symfony/event-dispatcher: ^6.4 || ^7.0
- symfony/http-foundation: ^6.4 || ^7.0
- symfony/http-kernel: ^6.4 || ^7.0
- symfony/var-exporter: ^6.4 || ^7.0
Requires (Dev)
- matthiasnoback/symfony-dependency-injection-test: ^4.3.1 || ^5.1
- phpspec/prophecy-phpunit: ^2.2
- phpunit/phpunit: ^10.5
- psalm/plugin-phpunit: ^0.19
- psalm/plugin-symfony: ^5.1
- setono/code-quality-pack: ^2.7
This package is auto-updated.
Last update: 2024-09-21 09:55:40 UTC
README
此包允许您在访问之间跟踪您的用户,并为每个用户添加自定义元数据。
默认情况下,该包将存储一个名为 setono_client_id
的cookie,其中包含客户端ID、创建时间戳和最后看到的时间戳。
它还会为每个客户端ID创建一个新的包含元数据的表。元数据功能是延迟加载的,所以如果您不使用它,它将不会查询数据库。
安装
要安装此包,只需运行
composer require setono/client-bundle
迁移您的数据库
php bin/console doctrine:migrations:diff php bin/console doctrine:migrations:migrate
使用方法
访问带有一些元数据的 Client
对象
use Setono\Client\Client; final class YourController extends AbstractController { public function index(Client $client): Response { return $this->render('your_template.html.twig', [ 'id' => $client->id, 'some_metadata' => $client->metadata->get('some_metadata_key'), // this call will initialize the metadata object from the database ]); } }
在事件订阅者中设置一些元数据
use Setono\Client\Client; use Symfony\Component\EventDispatcher\EventSubscriberInterface; use Symfony\Component\HttpKernel\KernelEvents; use Symfony\Component\HttpKernel\Event\RequestEvent; use Setono\ClientBundle\Context\ClientContextInterface; final class YourEventSubscriber implements EventSubscriberInterface { public function __construct(private readonly ClientContextInterface $clientContext) {} public static function getSubscribedEvents(): array { return [ KernelEvents::REQUEST => 'setMetadata', ]; } public function setMetadata(RequestEvent $event): void { if (!$event->isMainRequest() || !$event->getRequest()->query->has('gclid')) { return; } $this->clientContext->getClient()->metadata->set('google_click_id', $event->getRequest()->query->get('gclid')); } }
访问cookie
客户端ID存储在一个名为 setono_client_id
的cookie中(默认情况下)。该cookie还包含其他信息,如客户端首次和最后一次被看到的时间。您可以像这样访问cookie
use Setono\ClientBundle\CookieProvider\CookieProviderInterface; final class YourService { public function __construct(private readonly CookieProviderInterface $cookieProvider) {} public function call(): void { $cookie = $this->cookieProvider->getCookie(); if(null === $cookie) { return; // no cookie found } $clientId = $cookie->clientId; // the client id $created = $cookie->firstSeenAt; // the timestamp when the client was first seen $lastSeen = $cookie->lastSeenAt; // the timestamp when the client was last seen } }
配置
以下是您可以进行的配置
setono_client: cookie: # The name of the cookie that holds the client id. NOTICE that if you change this value, all clients with a cookie with the old name will be considered new clients name: setono_client_id # The expiration of the cookie. This is a string that can be parsed by strtotime expiration: '+365 days' # If you want to use a custom metadata class, you can specify it here metadata_class: Setono\ClientBundle\Entity\Metadata