mtconsultingroup/laravel-robots

robots.txt 生成服务。

2.1.2 2022-02-21 10:41 UTC

This package is not auto-updated.

Last update: 2024-09-30 23:15:46 UTC


README

Latest Version on Packagist Software License Build Status StyleCI Coverage Status Quality Score Total Downloads

优秀的robots.txt生成服务。更多详细信息请访问 www.robotstxt.org

安装

您可以通过composer安装此包

composer require mtconsultingroup/laravel-robots

用法

在您的 AppServiceProvider 中设置条件,以确定网站是否应该被索引。

use MadWeb\Robots\RobotsFacade;

public function boot()
{
    RobotsFacade::setShouldIndexCallback(function () {
        return app()->environment('production');
    });
    ...
}

您可以为生成robots.txt创建简单的单动作控制器

/routes/web.php

Route::get('robots.txt', 'RobotsController');

/app/Http/Controllers/Robots

namespace App\Http\Controllers;

use MadWeb\Robots\Robots;
use App\Http\Controllers\Controller;

class RobotsController extends Controller
{
    /**
     * Generate robots.txt
     */
    public function __invoke(Robots $robots)
    {
        $robots->addUserAgent('*');

        if ($robots->shouldIndex()) {
            // If on the live server, serve a nice, welcoming robots.txt.
            $robots->addDisallow('/admin');
            $robots->addSitemap('sitemap.xml');
        } else {
            // If you're on any other server, tell everyone to go away.
            $robots->addDisallow('/');
        }

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

在视图的 <head> 标签内添加robots元标签

<head>
	{!! Robots::metaTag() !!}
</head>

更新日志

请参阅 CHANGELOG 了解最近更改的详细信息。

测试

$ composer test

贡献

请参阅 CONTRIBUTINGCONDUCT 了解详细信息。

安全

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

鸣谢

许可证

MIT许可证(MIT)。请参阅 许可证文件 了解更多信息。