2017 © Pedro Peláez
 

cakephp-plugin cakephp-hits

Hits (view) counter for CakePHP 3 ORM.

image

kakaeriel/cakephp-hits

Hits (view) counter for CakePHP 3 ORM.

  • Friday, September 8, 2017
  • by kakaeriel
  • Repository
  • 1 Watchers
  • 0 Stars
  • 0 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 3 Forks
  • 0 Open issues
  • 1 Versions
  • 0 % Grown

The README.md

Hits

Build Status Coverage Total Downloads License, (*1)

Hits (view) counter for CakePHP 3 ORM., (*2)

Install

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');

Usage

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'
]);

Strategies

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)

Patches & Features

  • Fork
  • Mod, fix
  • Test - this is important, so it's not unintentionally broken
  • Commit - do not mess with license, todo, version, etc. (if you do change any, bump them into commits of their own that I can ignore when I pull)
  • Pull request - bonus point for topic branches

To ensure your PRs are considered for upstream, you MUST follow the CakePHP coding standards., (*18)

Bugs & Feedback

http://github.com/usemuffin/hits/issues, (*19)

License

Copyright (c) 2015, Use Muffin and licensed under The MIT License., (*20)

The Versions

08/09 2017

dev-master

9999999-dev https://github.com/kakaeriel/cakephp-hits

Hits (view) counter for CakePHP 3 ORM.

  Sources   Download

MIT

The Requires

 

The Development Requires

cakephp counter view count muffin kakaeriel