alibaba258/laravel-robots

通过简单方式管理机器人的 Laravel 扩展包

安装: 133

依赖者: 0

建议者: 0

安全: 0

星标: 0

关注者: 0

分支: 3

类型:laravel-package

v1.0.1 2023-03-20 11:49 UTC

This package is auto-updated.

Last update: 2024-09-20 14:50:41 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 文件以获取详细信息

作者