dev-master
9999999-dev https://github.com/kakaeriel/cakephp-hitsHits (view) counter for CakePHP 3 ORM.
MIT
The Requires
The Development Requires
by ADmad
by Jad Bitar
by Hairil Ibnuh
cakephp counter view count muffin kakaeriel
Hits (view) counter for CakePHP 3 ORM.
Hits (view) counter for CakePHP 3 ORM., (*2)
Using Composer:, (*3)
composer require muffin/hits:1.0.x-dev
You then need to load the plugin. You can use the shell command:, (*4)
bin/cake plugin load Muffin/Hits
or by manually adding statement shown below to bootstrap.php
:, (*5)
Plugin::load('Muffin/Hits');
Setting up the behavior is similar to the CounterCacheBehavior
in the sense that it takes
a list of fields as configuration. The field name could belong to the current table or to
another table (i.e. view_count
or OtherTable.posts_view_count
)., (*6)
$this->addBehavior('Muffin/Hits.Hits', ['view_count']);
Or more than just one field, the other one based on certain conditions:, (*7)
$this->addBehavior('Muffin/Hits.Hits', [ // count only if the post is published 'view_count' => ['is_published' => true], // count all views 'total_view_count' ]);
Or based on certain options passed to the Model.beforeFind
event (i.e. authenticated user):, (*8)
$this->addBehavior('Muffin/Hits.Hits', [ // count only if the user viewing it is not an admin 'view_count' => function (\Cake\ORM\Query $query, \ArrayObject $options, $counter) { return !isset($options['_footprint']) || $options['_footprint']->is_admin === false; }, // count all views 'total_view_count' ]);
You could also define the value to increment the counter by (defaults to 1
):, (*9)
$this->addBehavior('Muffin/Hits.Hits', [ 'view_count' => ['increment' => 2] ]);
To use them all at once:, (*10)
$this->addBehavior('Muffin/Hits.Hits', [ 'view_count' => [ 'conditions' => ['is_published' => true], 'callback' => function (\Cake\ORM\Query $query, \ArrayObject $options, $counter) { return !isset($options['_footprint']) || $options['_footprint']->is_admin === false; }, 'increment' => 2, ], 'total_view_count' ]);
Different strategies for keeping track of counts are made available., (*11)
DefaultStrategy(array $conditions = [], $offset = 1) (default), (*12)
This strategy, while maybe slow for some use cases, is the most widely used. It is also the only one that allows for extra conditions to be passed. Will hit the database on every increment operation., (*13)
CacheStrategy(CacheEngine $cache, $threshold = 100, $offset = 1), (*14)
This strategy is to be used for the most used counters. It will cache the counts by intervals and hit the database only once it hits the threshold., (*15)
SamplingStrategy(StrategyInterface $strategy, $size = 100), (*16)
To hit the database the lease and if precise numbers are not an issue, a common strategy used by big
sites is called sampling. It relies on a sample size to generate a random number before triggering
the increment
method on the strategy it wraps (which should have its threshold multiplied by the
sample size)., (*17)
To ensure your PRs are considered for upstream, you MUST follow the CakePHP coding standards., (*18)
http://github.com/usemuffin/hits/issues, (*19)
Copyright (c) 2015, Use Muffin and licensed under The MIT License., (*20)
Hits (view) counter for CakePHP 3 ORM.
MIT
cakephp counter view count muffin kakaeriel