Laravel Page View Counter 文章浏览统计
Laravel Page View Counter 文章浏览统计
简单粗暴添加文章阅读统计
精细化添加文章阅读统计统计
Package 改造添加文章阅读统计
1. 简单粗暴添加文章阅读统计
Post 添加统计字段
php artisan make:migration add_post_counter_to_post_table --table=post
Controller 添加增加
DB::table('post')->where('id', '=', $id)->increment('post_counter');
2.精细化添加文章阅读统计统计
略
3. Package 改造添加文章阅读统计
Visitor.php
Countable(interface)
Weboap\Visitor\Services\Cache\CacheInterface
Weboap\Visitor\Services\Geo\GeoInterface
Weboap\Visitor\Storage\VisitorInterface
VisitorInterface
create
get
update
delete
all
count
clicksSum
range
increment
QbVisitorRepository implements VisitorInterface
1. $tableName $config $config 2. __construct(Config $config, DB $db) 3. setTable($table) 4. getTable() 5. create(array $data) 6. get($ip) 7. update($ip, array $data) 8. delete($ip) 9. all() 10. increment($ip) * $this->db->table($this->getTable())->whereIp($ip)->increment('clicks'); 11. clicksSum() 12. range($start, $end) 13. clear()
CacheInterface.php
use Illuminate\Cache\CacheManager as Cache;
__construct(Cache $cache)
destroy($key)
rememberForever($key, $data)
GeoInterface
use GeoIp2\Database\Reader;
use GeoIp2\Exception\AddressNotFoundException;
use Illuminate\Config\Repository as Config;
use Weboap\Visitor\Ip;
locate()
ValidationInterface
validate($ip)
Visitor.php
get($ip = null)
log()
forget($ip)
has($ip)
count($ip = null)
all($collection = false)
4. Reference
[Implementing A Page View Counter In Laravel]()