PHPackages                             muffin/hits - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. muffin/hits

ActiveCakephp-plugin

muffin/hits
===========

Hits (view) counter for CakePHP 3 ORM.

61506PHP

Since Jan 27Pushed 10y ago3 watchersCompare

[ Source](https://github.com/UseMuffin/Hits)[ Packagist](https://packagist.org/packages/muffin/hits)[ RSS](/packages/muffin-hits/feed)WikiDiscussions master Synced 2mo ago

READMEChangelogDependenciesVersions (1)Used By (0)

Hits
====

[](#hits)

[![Build Status](https://camo.githubusercontent.com/00087b8224cb2bdd8c1ba09b839a375388015684501ae46864350987a9b1b8cc/68747470733a2f2f696d672e736869656c64732e696f2f7472617669732f5573654d756666696e2f486974732f6d61737465722e7376673f7374796c653d666c61742d737175617265)](https://travis-ci.org/UseMuffin/Hits)[![Coverage](https://camo.githubusercontent.com/6aee6022dd048550e8c522b8996ff6a04dfc1282a1892ecbe9ffd59bb6af891f/68747470733a2f2f696d672e736869656c64732e696f2f636f766572616c6c732f5573654d756666696e2f486974732f6d61737465722e7376673f7374796c653d666c61742d737175617265)](https://coveralls.io/r/UseMuffin/Hits)[![Total Downloads](https://camo.githubusercontent.com/50bef39b64d839804052e25fd66cbfb85ed9524eae6fd30a497a55bd87f5c67b/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64742f6d756666696e2f686974732e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/muffin/hits)[![License](https://camo.githubusercontent.com/942e017bf0672002dd32a857c95d66f28c5900ab541838c6c664442516309c8a/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f6c6963656e73652d4d49542d626c75652e7376673f7374796c653d666c61742d737175617265)](LICENSE)

Hits (view) counter for CakePHP 3 ORM.

Install
-------

[](#install)

Using [Composer](http://getcomposer.org):

```
composer require muffin/hits:1.0.x-dev
```

You then need to load the plugin. You can use the shell command:

```
bin/cake plugin load Muffin/Hits
```

or by manually adding statement shown below to `bootstrap.php`:

```
Plugin::load('Muffin/Hits');
```

Usage
-----

[](#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`).

```
$this->addBehavior('Muffin/Hits.Hits', ['view_count']);
```

Or more than just one field, the other one based on certain conditions:

```
$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):

```
$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`):

```
$this->addBehavior('Muffin/Hits.Hits', [
    'view_count' => ['increment' => 2]
]);
```

To use them all at once:

```
$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

[](#strategies)

Different strategies for keeping track of counts are made available.

**DefaultStrategy(array $conditions = \[\], $offset = 1)** (default)

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.

**CacheStrategy(CacheEngine $cache, $threshold = 100, $offset = 1)**

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.

**SamplingStrategy(StrategyInterface $strategy, $size = 100)**

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).

Patches &amp; Features
----------------------

[](#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](http://book.cakephp.org/3.0/en/contributing/cakephp-coding-conventions.html).

Bugs &amp; Feedback
-------------------

[](#bugs--feedback)

License
-------

[](#license)

Copyright (c) 2015, [Use Muffin](http://usemuffin.com) and licensed under [The MIT License](http://www.opensource.org/licenses/mit-license.php).

###  Health Score

25

—

LowBetter than 37% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity18

Limited adoption so far

Community15

Small or concentrated contributor base

Maturity41

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 94.1% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

### Community

Maintainers

![](https://www.gravatar.com/avatar/ccb57ae849b67ade8d9c1e266bc2868183bbcf07fb6c66c2d45254a452272d54?d=identicon)[jadb](/maintainers/jadb)

![](https://www.gravatar.com/avatar/e31753bdd616948c7c8978ea9b5805378f75bfa62564e69c0aa2fd67aaf418c5?d=identicon)[ADmad](/maintainers/ADmad)

---

Top Contributors

[![jadb](https://avatars.githubusercontent.com/u/33527?v=4)](https://github.com/jadb "jadb (16 commits)")[![josegonzalez](https://avatars.githubusercontent.com/u/65675?v=4)](https://github.com/josegonzalez "josegonzalez (1 commits)")

### Embed Badge

![Health badge](/badges/muffin-hits/health.svg)

```
[![Health](https://phpackages.com/badges/muffin-hits/health.svg)](https://phpackages.com/packages/muffin-hits)
```

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
