fr3nch13 / cakephp-stats
CakePHP项目的统计插件
0.3
2023-10-26 21:58 UTC
Requires
- cakephp/cakephp: ~5.0
- cakephp/migrations: ~4.1
- cakephp/plugin-installer: ~2.0
Requires (Dev)
- cakephp/cakephp-codesniffer: ~5.1
- phpstan/phpstan: ~1.10
- phpunit/phpunit: ~10.4
- rexxars/html-validator: ^2.3
Suggests
- bower-asset/chartjs: If you want to use the templates/element/chartjs/* elements. See: https://asset-packagist.org
README
用于跟踪和显示统计数据,以及趋势。
安装
您可以使用 composer 将此插件安装到您的CakePHP应用程序中。
安装composer包的推荐方式是
composer require fr3nch13/cakephp-stats
使用
这一切都围绕着事件监听器 StatsListener
。
要使用此插件,您需要扩展 StatsListener
,并在其中定义您的 StatsObject
keys
。以下是如何扩展 StatsListener
并定义您的 objects
的示例。
src/Event/TestListener.php
<?php declare(strict_types=1); namespace App\Event; use Cake\Event\Event; class ArticleListener extends StatsListener { // Define your events here public function implementedEvents(): array { return [ 'App.Article.hit' => 'onHit', ]; } public function onHit(Event $event, int $articleId, int $count = 1): bool { // track if any articles were viewed // Article.hits is a StatsObject key. parent::recordCount($event, 'Articles.hits'); // leave out count to just increment by one. // track the specific article // Article.hits.[id] is a seperate StatsObject key from above. parent::recordCount($event, 'Articles.hits.' . $articleId, $count); } }
一旦您已创建从 StatsListener
扩展的监听器,您需要注册它。无论是直接在应用程序中使用(在 Application.php
中),还是在其他插件中使用(在 Plugin.php
中),您都需要在 bootstrap()
方法中使用事件管理器注册您的监听器。例如,见:StatsPlugin.php
的 bootstrap()
。
src/BlogPlugin.php
<?php declare(strict_types=1); namespace Fr3nch13\Blog; use Cake\Core\BasePlugin; use Cake\Core\PluginApplicationInterface; use Cake\Event\EventManager; use Fr3nch13\Stats\Event\TestListener; class BlogPlugin extends BasePlugin { // other code public function bootstrap(PluginApplicationInterface $app): void { // Register your listener with the Event Manager EventManager::instance()->on(new ArticleListener()); parent::bootstrap($app); } /// other code }
src/Controller/ArticlesController.php
<?php declare(strict_types=1); namespace Fr3nch13\Blog\Controller; use Cake\Event\Event; use Fr3nch13\Blog\AppController; class ArticlesController extends AppController { /** * Example of how to register a hit */ public function view(int $id): ?Response { $article = $this->Articles->get($id); // do this reight before rendering the view incase your code above throws an error, // or redirects somewhere else. $this->getEventManager()->dispatch(new Event('App.Article.hit', $this, [ 'articleId' => $id, 'count' => 1, ])); } }
要使用控制器特质,您可以这样做
src/Controller/Admin/ArticlesController.php
<?php declare(strict_types=1); namespace Fr3nch13\Blog\Admin\Controller; use Fr3nch13\Stats\Controller\ChartJsTrait; use Fr3nch13\Blog\Admin\AppController; class ArticlesController extends AppController { /** * Used to do the common tasks for chartjs graphs. */ use ChartJsTrait; // other code public function line(?int $range = null, ?string $timeperiod = null): ?Response { $keys = [ 'Articles.hits', 'Articles.hits.1', 'Articles.hits.2', 'Articles.hits.3', ]; return $this->chartJsLine($keys, $range, $timeperiod); } // other code /** * To get the stats in a dashboard * * @return ?\Cake\Http\Response Renders view */ public function dashboard(): ?Response { /** @var \Fr3nch13\Stats\Model\Table\StatsCountsTable $StatsCounts */ $StatsCounts = $this->getTableLocator()->get('Fr3nch13/Stats.StatsCounts'); $stats = $this->StatsCounts->getObjectStats('Articles.hits'); /* $stats will look like: $stats = [ 'year' => 12001, <-- counts 'month' => 3001, 'week' => 701, 'day' => 101, 'hour' => 11, ]; */ $this->set(compact('stats')); $this->viewBuilder()->setOption('serialize', ['stats']); return null; } }