nmure/crawler-detect-bundle

一个用于Crawler-Detect库的Symfony扩展包(通过用户代理检测机器人/爬虫/蜘蛛)

安装数: 257,786

依赖者: 0

建议者: 0

安全: 0

星标: 25

关注者: 5

分支: 12

开放性问题: 1

类型:symfony-bundle

v2.0.0 2017-12-02 20:49 UTC

This package is not auto-updated.

Last update: 2024-09-19 17:00:47 UTC


README

Build Status Coverage Status

这是一个用于Crawler-Detect库的Symfony扩展包(通过用户代理检测机器人/爬虫/蜘蛛)。

目录

简介

此扩展包将Crawler-Detect库集成到Symfony中。在继续阅读此处之前,建议您先阅读库的文档。

此扩展包的目标是公开CrawlerDetect类作为服务(crawler_detect),以便更容易与Symfony一起使用(依赖注入,从控制器中可用等)。

安装

使用composer下载扩展包

$ composer require nmure/crawler-detect-bundle "^2.0.0"

对于Symfony < 4.0,运行

$ composer require nmure/crawler-detect-bundle "^1.0.0"

然后在您的AppKernel中启用扩展包

// app/AppKernel.php
class AppKernel extends Kernel
{
    public function registerBundles()
    {
        $bundles = array(
            // ...
            new Nmure\CrawlerDetectBundle\CrawlerDetectBundle(),
            // ...
        );
    }
}

使用

crawler_detect服务使用Symfony主请求的数据初始化。

从控制器中使用此服务

public function indexAction()
{
    if ($this->get('crawler_detect')->isCrawler()) {
        // this request is from a crawler :)
    }

    // you can also specify an user agent if you don't want
    // to use the one of the master request or if the app
    // is accessed by the CLI :
    $ua = 'Mozilla/5.0 (compatible; Googlebot/2.1; +http://www.google.com/bot.html)';
    if ($this->get('crawler_detect')->isCrawler($ua)) {
        // this user agent belongs to a crawler :)
    }
}

您还可以使用crawler_detect服务ID将此服务注入为依赖项。

测试

$ docker run --rm -v `pwd`:/app phpunit/phpunit -c /app