kryptonit3 / counter
页面点击计数器。
Requires
- php: >=5.3.3
- illuminate/support: 5.*
- jaybizzle/crawler-detect: 1.*
- ramsey/uuid: ~3.0
README
安装
运行以下命令
composer require kryptonit3/counter:5.2.*
将以下内容添加到您的 config\app.php
服务提供者
Kryptonit3\Counter\CounterServiceProvider::class,
将以下内容添加到您的 config\app.php
门面
'Counter' => Kryptonit3\Counter\Facades\CounterFacade::class,
然后运行以下命令
php artisan vendor:publish --provider="Kryptonit3\Counter\CounterServiceProvider" --tag="migrations" php artisan migrate
在 Laravel env 配置文件中设置 Cookie 名称
COUNTER_COOKIE="Name of your cookie"
如何使用
常规页面
只需添加 Counter::showAndCount('home')
(对于 blades 使用 {{ Counter::showAndCount('home') }}
)。将 home
更改为您正在处理的页面的唯一名称。
动态页面
对于动态页面,如用户资料或职位列表等,您可以提供如下动态元素 Counter::showAndCount('user-profile', $user->id)
(对于 blades 使用 {{ Counter::showAndCount('user-profile', $user->id) }}
)
将
user-profile
更改为您正在处理的页面的唯一名称。
数字输出已格式化。因此,3000 访问者将显示为 3,000
如果您在页面上有记录,但不想将显示记录的页面计数为点击记录本身,则将 showAndCount
更改为仅 show
。例如 Counter::show('job-listings', $job->id)
(对于 blades 使用 {{ Counter::show('job-listings', $job->id) }}
)
您可以使用 Counter::allHits()
获取整个网站上每个页面的所有点击(对于 blades 使用 {{ Counter::allHits() }}
)。要指定日期约束,例如仅过去 30 天的所有点击,请执行 Counter::allHits(30)
(对于 blades 使用 {{ Counter::allHits(30) }}
)。
如果您只想为页面处理点击而不显示任何内容,则只需使用 Counter::count('user-profile', $user->id)
。它与所有以前的示例对于静态和动态页面都一样,blade 语法相同。对于在不让每个人都看到的情况下计数页面点击非常有用。
享受吧!
额外
- 受以下包的影响: defuse/phpcount
- 与以下内容的区别: weboAp/Visitor
此包允许您查看特定页面/对象的点击数以及整个站点的点击数。它还使用唯一生成的 Cookie(回退到 IP)以提供更准确的读取。适合喜欢匿名的人的不错包 :)
mysql> select * from kryptonit3_counter_page; +----+--------------------------------------+ | id | page | +----+--------------------------------------+ | 2 | 24d83c12-a1e0-598b-93ee-df05ae3f87e7 | | 1 | 597e0526-152f-5fc0-9d44-b51fd9e45b8f | +----+--------------------------------------+ 2 rows in set (0.00 sec) mysql> select * from kryptonit3_counter_visitor; +----+------------------------------------------------------------------+ | id | visitor | +----+------------------------------------------------------------------+ | 1 | 88a5f67524a1bc75da5ea8b7250e8280c78d60dce59b129dc37123b137ce6199 | +----+------------------------------------------------------------------+ 1 row in set (0.00 sec) mysql> select * from kryptonit3_counter_page_visitor; +---------+------------+---------------------+ | page_id | visitor_id | created_at | +---------+------------+---------------------+ | 1 | 1 | 2015-06-22 17:52:43 | | 2 | 1 | 2015-06-22 17:52:43 | +---------+------------+---------------------+ 2 rows in set (0.00 sec)