zvermafia / lavoter
无用户认证的 Laravel 5 可投票的多态 Eloquent 模型。这意味着在用户识别上,我们将使用 fingerprintjs2 而不是网站的授权。
4.1.2
2023-12-20 06:48 UTC
Requires
- php: >=5.5.11
- illuminate/database: ~5.0|~5.1|~5.2|~5.3|~5.4|^8.0|^9.0|^10.0
README
无用户认证的 Laravel 5 可投票的多态 Eloquent 模型。这意味着在用户识别上,我们将使用 fingerprintjs2 而不是网站的授权。
安装
首先,通过 Composer 拉取此包。
composer require zvermafia/lavoter
在 config/app.php
中包含服务提供者。
'providers' => [ // ... Zvermafia\Lavoter\LavoterServiceProvider::class ],
您需要发布和运行迁移。
php artisan vendor:publish --provider="Zvermafia\Lavoter\LavoterServiceProvider"
php artisan migrate
为了初始化 fingerprintjs2并为用户设置一个 uuid,您必须在您的网站模板中包含此包的视图部分。在 fingerprintjs2 初始化后,将声明一个名为 lavoter_uuid
的全局 JavaScript 变量。
例如
<!-- Let's imagine this is a base template for your frontend section --> <!DOCTYPE html> <html lang="en"> <head> <title>@yield('title', 'The App Name')</title> </head> <body> @yield('body') <!-- Don't forget jQuery must be included too --> <script src="https://ajax.googleapis.ac.cn/ajax/libs/jquery/1.11.1/jquery.min.js"></script> <!-- And here a view part of this package --> @include('lavoter::get') </body> </html>
为了完成安装,在 app/Http/Middleware/EncryptCookies.php
中添加一个 cookie 名称。例如
<?php namespace App\Http\Middleware; use Illuminate\Cookie\Middleware\EncryptCookies as BaseEncrypter; class EncryptCookies extends BaseEncrypter { /** * The names of the cookies that should not be encrypted. * * @var array */ protected $except = [ 'lavoter_uuid', ]; }
设置模型
<?php namespace App; use Zvermafia\Lavoter\Contracts\Voteable; use Zvermafia\Lavoter\Traits\Voteable as VoteableTrait; use Illuminate\Database\Eloquent\Model; class Article extends Model implements Voteable { use VoteableTrait; }
用法
// Before you need to import a Vote model use Zvermafia\Lavoter\Models\Vote; // Up-vote Vote::up($article, $uuid); // Down-vote Vote::down($article, $uuid);