maher / laravel-counters
laravel系统中计数器的管理
Requires
- php: >=7.0
This package is not auto-updated.
Last update: 2024-09-23 04:26:30 UTC
README
在某些情况下,您需要在您的laravel项目中管理计数器的状态,例如网站访客数量、文章查看次数或文件下载次数,这需要创建一个新的表来保存这些记录,或者至少为您的表添加一个新的列来保存计数值。
因此,使用此软件包,您可以为您的模型创建任意数量的计数器,而无需在模型表中创建物理列。此外,您还可以存储公共计数器,如网站的"number_of_visitors",而无需创建整个表来存储它。
总之,此软件包允许您在laravel项目中管理计数器。
安装后,您可以进行如下操作
//increment/decrement system counters Counters::increment('number_of_visitors'); // increment/decrement model objects counters $post->incrementCounter('number_of_views'); $feature->decrementCounter('number_of_bugs'); $user->decrementCounter('balance', 2); // decrement the balance of the user by 2
以下列出了许多其他方法。
安装
Laravel
此软件包可用于Laravel 5.4或更高版本。如果您正在使用较旧版本的Laravel,您可以通过composer安装此软件包
composer require maher/laravel-counters "@dev"
在Laravel 5.5及更高版本中,服务提供程序将自动注册。在框架的较旧版本中,只需将服务提供程序添加到config/app.php
文件中即可
'providers' => [ // ... Maher\Counters\CountersServiceProvider::class, //... ];
您必须使用以下方式发布迁移
php artisan vendor:publish --provider="Maher\Counters\CountersServiceProvider" --tag="migrations"
在迁移发布后,您可以通过运行迁移来创建表
php artisan migrate
您可以通过以下方式发布配置文件
php artisan vendor:publish --provider="Maher\Counters\CountersServiceProvider" --tag="config"
用法
1) 使用无模型的计数器
首先,将Maher\Counters\Traits\HasCounter
特性添加到您的模型中:例如,我们可以将其添加到Post模型中
use Maher\Counters\Traits\HasCounter; class Post extends Model { use HasCounter; // ... }
此软件包允许帖子与计数器相关联。每个帖子都可以关联多个计数器。
我们可以创建一个计数器,例如,让我们为Post模型创建一个查看次数计数器。
use Maher\Counters\Models\Counter; $counter = Counter([ 'key' => 'number_of_views', 'name' => 'Views', 'initial_value' => 0 //(could be left empty, default value is 0) 'step' => 1 // (could be left empty, default value is 1) ]);
之后,例如,在帖子控制器的show函数中,您可以添加以下行
class PostsController extends Controller { public function show(Post $post) { //... $post->incrementCounter('number_of_views'); //... } }
通过这样做,每次显示帖子时,每个帖子的number_of_views计数器都将增加[步长]。
此软件包还有其他功能。
// will return the counter object $post->getCounter($key); // will return the counter value $post->getCounterValue($key); //will add record in countrable table for this post object $post->addCounter($key); //will remove the record from countrable table for this post object. $post->removeCounter($key); //increment the counter with the given $key //Note that this will create record in countrable table,if it's not exist //if $step is entered, it will increment with the value of $step $post->incrementCounter($key, $step = null); //decrement the counter with the given $key //Note that this will create record in countrable table,if it's not exist //if $step is entered, it will decrement with the value of $step $post->decrementCounter($key, $step = null); // will reset the counter value (to initial_value) for the post object. $post->resetCounter($key);
2) 使用无模型的计数器。
有时,您有与任何模型都不关联的通用计数器,例如网站的访客数量。
因此,此软件包将允许您处理此类类型的计数器。
use Maher\Counters\Facades\Counters; class Test { public function incrementFunction() { //moreover you can add this function in your public page to be incremented //every time user hits your website Counters::increment('number_of_visitors'); } }
此外观有许多其他功能
// will return the counter object Counters::get($key); // will return the counter value Counters::getValue($key, $default = null); // set the value of the counter Counters::setValue($key, $value); //set the step of the counter Counters::setStep($key, $step); //increment the counter with the given $key Counters::increment($key, $step = null); //decrement the counter with the given $key Counters::decrement($key, $step = null); // will reset the counter for the inital_value Counters::reset($key);
在某些情况下,您希望为每个人只增加计数器一次,例如,不需要每次同一用户刷新页面时都增加number_of_visitors计数器。
因此,您可以使用这些函数
Counters::incrementIfNotHasCookies($key); Counters::decrementIfNotHasCookies($key);
3)使用 artisan 命令
您可以使用 artisan 命令从控制台创建计数器。以下命令创建一个名为number_of_visitors的计数器,初始值为0,步长为1
php artisan make:counter number_of_visitors Visitors 0 1
4) 使用计数器的 API
在某些情况下,我们正在使用单页应用程序,或者我们不想离开当前页面。因此,有API可以增加/减少通用/模型对象计数器。
示例
use Maher\Counters\Facades\Counters; //this will return a link to increment the counter key //for exampel 'exmple.com/counters/increment/visitors' Counters::getIncrementUrl($key); Counters::getDecrementUrl($key); // we can use these in Blades for example, // <a href={{\Counters::getIncrementUrl($key)}}>Incrment Visitors</a>
通过使用此链接,将返回更新计数器的JSON结构作为响应
{ "counter": { "id": 1, "key": "visitors", "name": "Visitors", "initial_value": 0, "value": 9, "step": 3, "notes": null, "created_at": "2018-07-02 20:57:03", "updated_at": "2018-07-07 13:07:49" } }
此外,我们可以通过这些方法增加/减少对象计数器
$post = Post::find(1); $post->getIncrementUrl($key); $post->getDecrementUrl($key);
将返回如下JSON结构作为响应
{ "counterable": { "id": 2, "counterable_id": 2, "counterable_type": "App\\Post", "counter_id": 1, "value": 24, "created_at": null, "updated_at": "2018-07-07 13:09:41", "counter": { "id": 1, "key": "visitors", "name": "Visitors", "initial_value": 0, "value": 9, "step": 3, "notes": null, "created_at": "2018-07-02 20:57:03", "updated_at": "2018-07-07 13:07:49" } } }
数据库初始化
以下是一个示例种子。
use Illuminate\Database\Seeder; use Maher\Counters\Facades\Counters; class CounterTableSeeder extends Seeder { public function run() { // create Counters //This will create a counter with inital value as 3, and every increment 5 will be added. Counter::create([ 'key' => 'number_of_visitors', 'name' => 'Visitors', 'initial_value' => 3, 'step' => 5 ]); //This counter will has 0 as inital_value and 1 as step Counter::create([ 'key' => 'number_of_visitors2', 'name' => 'Visitors2' ]); $viewCounter = Counter::create([ 'key' => 'number_of_views', 'name' => 'Views' ]); $post = Post::find(1); $post->addCounter('number_of_views');// to add the record to countrable table } }
致谢
马赫·赫德里尔
领英
邮箱: maher.khdeir@gmail.com
特别感谢
特别感谢 Spatie ,因为我从那里学习并遵循了构建Laravel包的结构。
许可证
MIT许可证(MIT)。有关更多信息,请参阅 许可证文件。