mguinea/laravel-robots

用于轻松管理机器人的Laravel包

安装次数: 38,733

依赖关系: 0

建议者: 0

安全性: 0

星标: 16

关注者: 3

分支: 3

公开问题: 0

类型:laravel-package

3.1.1 2021-02-22 21:06 UTC

This package is auto-updated.

Last update: 2024-08-30 01:17:37 UTC


README

Laravel包,用于轻松管理机器人。

如果您需要详细了解robots.txt文件的工作原理,请访问http://www.robotstxt.org/robotstxt.html

Buy Me A Coffee

Scrutinizer Code Quality Code Coverage Build Status StyleCI License MIT Laravel

此包允许您动态管理网站上的机器人,您可以根据不同的环境或配置进行区分。

迁移以持久化配置是可选的;您可以更改其数据源。

一旦安装了包,您就可以做以下事情

Route::get('robots.txt', function() {
    $robots = new \Mguinea\Robots\Robots;

    // If on the live server
    if (App::environment() == 'production') {
        $robots->addUserAgent('*')->addSitemap('sitemap.xml');
    } else {
        // If you're on any other server, tell everyone to go away.
        $robots->addDisallow("/");
    }

    return response($robots->generate(), 200)->header('Content-Type', 'text/plain');
});

安装

您可以通过Composer进行安装。

composer require mguinea/laravel-robots

运行测试

只需执行

vendor/bin/phpunit

单元测试将测试Robots类及其相关外观的所有方法。

使用方法

1. 动态方式

您可以在路由文件中使用Robots生成动态响应

Route::get('robots.txt', function() {
    $robots = new \Mguinea\Robots\Robots;

    // If on the live server
    if (App::environment() == 'production') {
        $robots->addUserAgent('*')->addSitemap('sitemap.xml');
    } else {
        // If you're on any other server, tell everyone to go away.
        $robots->addDisallow("/");
    }

    return response($robots->generate(), 200)->header('Content-Type', 'text/plain');
});

1.1. 使用外观动态

您可以在路由文件中使用Robots外观生成动态响应

<?php

use Mguinea\Robots\Facades\Robots;

Route::get('robots.txt', function() {

    // If on the live server
    if (App::environment() == 'production') {
        Robots::addUserAgent('*');
        Robots::addSitemap('sitemap.xml');
    } else {
        // If you're on any other server, tell everyone to go away.
        Robots::addDisallow("/");
    }

    return response(Robots::generate(), 200)->header('Content-Type', 'text/plain');
});

2. 生成默认robots.txt文件

如果您喜欢编写原始的robots.txt文件,只需像您所看到的那样使用生成器即可

<?php

use Illuminate\Http\File;
use Mguinea\Robots\Robots;

class Anywhere
{
    public function createFile()
    {
        $robots = new Robots;
        $robots->addUserAgent('*')->addSitemap('sitemap.xml');

        File::put(public_path('robots.txt'), $robots->generate());
    }
}

从数据源构建

您可能更喜欢从某些数据源构建它。为此,您只需使用以下示例所示键值参数的数组实例化Robots对象。

注意,已移除注释和空白符。

<?php

use Illuminate\Http\File;
use Mguinea\Robots\Robots;

class Anywhere
{
    public function fromArray()
    {
        $robots = new Robots([
            'allows' => [
                'foo', 'bar'
            ],
            'disallows' => [
                'foo', 'bar'
            ],
            'hosts' => [
                'foo', 'bar'
            ],
            'sitemaps' => [
                'foo', 'bar'
            ],
            'userAgents' => [
                'foo', 'bar'
            ],
            'crawlDelay' => 10
        ]);
        
        return response($robots->generate(), 200)->header('Content-Type', 'text/plain');
    }
}

方法

您可以以独立或嵌套的方式使用Robots类方法。

请记住,您可以使用外观来避免实例化。

<?php
    // Add an allow rule to the robots. Allow: foo
    $robots->addAllow('foo');

    // Add multiple allows rules to the robots. Allow: foo Allow: bar
    $robots->addAllow(['foo', 'bar']);
<?php
    // Add a comment to the robots. # foo
    $robots->addComment('foo');
<?php
    // Add a disallow rule to the robots. Disallow: foo
    $robots->addDisallow('foo');

    // Add multiple disallows rules to the robots. Disallow: foo Disallow: bar
    $robots->addDisallow(['foo', 'bar']);
<?php
    // Add a Host to the robots. Host: foo
    $robots->addHost('foo');
    
    // Add multiple hosts to the robots. Host: foo Host: bar
    $robots->addHost(['foo', 'bar']);
<?php
    // Add a Sitemap to the robots. Sitemap: foo
    $robots->addSitemap('foo');
    
    // Add multiple sitemaps to the robots. Sitemap: foo Sitemap: bar
    $robots->addSitemap(['foo', 'bar']);
<?php
    // Add a spacer to the robots.
    $robots->addSpacer();
<?php
    // Add a User-agent to the robots. User-agent: foo
    $robots->addUserAgent('foo');
    
    // Add multiple User-agents to the robots. User-agent: foo User-agent: bar
    $robots->addUserAgent(['foo', 'bar']);
<?php
    // Add a crawl-delay to the robots. crawl-delay: 10
    $robots->addCrawlDelay(10);
<?php
    // Generate the robots data.
    $robots->generate();
<?php
    // Reset the rows.
    $robots->reset();

构建工具

贡献

请阅读CONTRIBUTING.md以了解我们的行为准则以及提交拉取请求的过程。

安全性

如果您发现任何与安全相关的问题,请通过develop.marcguinea@gmail.com发送电子邮件,而不是使用问题跟踪器。

版本控制

我们使用SemVer进行版本控制。有关可用的版本,请参阅此存储库上的标签

许可

本项目采用MIT许可证 - 有关详细信息,请参阅LICENSE文件。

作者