xlabs / likebundle
1.0.31
2022-10-18 10:08 UTC
Requires
- php: >=5.6
- predis/predis: ^1.1
- symfony/symfony: >=3.4
- xlabs/rabbitmqbundle: ^1.0
Requires (Dev)
- symfony/asset: ~2.7|~3.0.0
- symfony/console: ~2.8|~3.0.0
- symfony/expression-language: ~2.4|~3.0.0
- symfony/finder: ~2.3|~3.0.0
- symfony/form: ^2.8.23
- symfony/http-kernel: ~2.8|~3.0.0
- symfony/polyfill-intl-icu: ~1.0
- symfony/routing: ~2.2|~3.0.0
- symfony/security: ~2.6|~3.0.0
- symfony/security-acl: ~2.6|~3.0.0
- symfony/stopwatch: ~2.2|~3.0.0
- symfony/templating: ~2.1|~3.0.0
- symfony/translation: ~2.7|~3.0.0
- symfony/var-dumper: ~2.7.16|~2.8.9|~3.0.9
- symfony/yaml: ^2.0.5|~3.0.0
Suggests
- symfony/asset: For using the AssetExtension
- symfony/expression-language: For using the ExpressionExtension
- symfony/finder
- symfony/form: For using the FormExtension
- symfony/http-kernel: For using the HttpKernelExtension
- symfony/routing: For using the RoutingExtension
- symfony/security: For using the SecurityExtension
- symfony/stopwatch: For using the StopwatchExtension
- symfony/templating: For using the TwigEngine
- symfony/translation: For using the TranslationExtension
- symfony/var-dumper: For using the DumpExtension
- symfony/yaml: For using the YamlExtension
This package is auto-updated.
Last update: 2024-09-18 14:08:54 UTC
README
由redis驱动的点赞引擎。
安装
通过composer安装
php -d memory_limit=-1 composer.phar require xlabs/likebundle
此包依赖于"xlabs/rabbitmqbundle"。请确保也设置它。
在你的AppKernel中
public function registerbundles()
{
return [
...
...
new XLabs\LikeBundle\XLabsLikeBundle(),
];
}
php bin/console doctrine:schema:update --force
路由
追加到主路由文件
# app/config/routing.yml
xlabs_like_engine:
resource: "@XLabsLikeBundle/Resources/config/routing.yml"
#prefix: /
配置示例
以下显示默认值
# app/config/config.yml
x_labs_like:
redis_settings:
host: 192.168.5.23
port: 6379
database_id: 7
_key_namespace: 'xlabs:like'
_star_key_namespace: 'xlabs:star'
backup: # for mysql backup/restore
<alias>: <entity_FQCN>
star_rating:
amount: 5 # amount of stars, 5 is the default
assetic:
...
bundles: [ ..., 'XLabsLikeBundle']
...
用法
将此代码添加到模板中的任何位置
{% include 'XLabsLikeBundle:Like:loader.html.twig' %}
要查看示例模板,请检查
XLabsLikeBundle:Like:example.html.twig
MySQL备份
确保运行以下命令。这是将所有操作保存到项目数据库中的消费者。
php bin/console xlabs:like:mysql_backup --no-debug
如果Redis丢失了所有数据,您可以通过以下命令恢复它
php bin/console xlabs:like:restore --no-debug
如果您已经在redis中有了数据,并想创建它的mysql备份,这里有一个一次性的示例命令,您应该将其复制到您的项目中并相应地调整
php bin/console xlabs:like:initial_backup --no-debug
事件监听器
如果您想在前端每次发生LIKE操作时执行某些操作,您可以创建一个事件监听器,如下所示
# YourBundle/Resources/config/services.yml
...
xlabs_like.event_listener:
class: YourBundle\EventListeners\YourListener.php
tags:
- { name: kernel.event_listener, event: xlabs_like.event, method: yourListenerMethod }
use Symfony\Component\EventDispatcher\Event;
class YourListener extends Event
{
public function yourListenerMethod(Event $event)
{
dump($event->getLike()); die;
}
}
$event变量包含有关已发生的LIKE操作的所有信息。
调整
默认情况下,服务使用会话中的用户。如果您希望使用自己的用户执行LIKE操作以使用此服务
$user = $em->getRepository('YourBundle:YourUserEntity')->find(<ID>);
$like_engine = $container->get('xlabs_like_engine');
$like_engine->setUser($user);
$like_engine->...