├── .github ├── FUNDING.yml ├── ISSUE_TEMPLATE │ └── config.yml └── workflows │ ├── php-cs-fixer.yml │ ├── update-changelog.yml │ └── run-tests.yml ├── src ├── InvalidIndexRule.php └── RobotsMiddleware.php ├── .editorconfig ├── .phpunit.cache └── test-results ├── LICENSE.md ├── composer.json ├── .php_cs.dist.php ├── CHANGELOG.md └── README.md /.github/FUNDING.yml: -------------------------------------------------------------------------------- 1 | custom: https://spatie.be/open-source/support-us 2 | -------------------------------------------------------------------------------- /src/InvalidIndexRule.php: -------------------------------------------------------------------------------- 1 | response = $next($request); 15 | 16 | $shouldIndex = $this->shouldIndex($request); 17 | 18 | if (is_bool($shouldIndex)) { 19 | return $this->responseWithRobots($shouldIndex ? 'all' : 'none'); 20 | } 21 | 22 | if (is_string($shouldIndex)) { 23 | return $this->responseWithRobots($shouldIndex); 24 | } 25 | 26 | throw InvalidIndexRule::requiresBooleanOrString(); 27 | } 28 | 29 | protected function responseWithRobots(string $contents) 30 | { 31 | $this->response->headers->set('x-robots-tag', $contents, false); 32 | 33 | return $this->response; 34 | } 35 | 36 | /** 37 | * @return string|bool 38 | */ 39 | protected function shouldIndex(Request $request) 40 | { 41 | return true; 42 | } 43 | } 44 | -------------------------------------------------------------------------------- /LICENSE.md: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) Spatie bvba 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in 13 | all copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 21 | THE SOFTWARE. 22 | -------------------------------------------------------------------------------- /.github/workflows/run-tests.yml: -------------------------------------------------------------------------------- 1 | name: run-tests 2 | 3 | on: 4 | - push 5 | - pull_request 6 | 7 | jobs: 8 | test: 9 | runs-on: ${{ matrix.os }} 10 | 11 | strategy: 12 | fail-fast: true 13 | matrix: 14 | os: [ubuntu-latest] 15 | php: [8.2] 16 | laravel: ['10.*', '11.*', '12.*'] 17 | dependency-version: [prefer-stable] 18 | 19 | name: P${{ matrix.php }} - L${{ matrix.laravel }} - ${{ matrix.dependency-version }} - ${{ matrix.os }} 20 | 21 | steps: 22 | - name: Checkout code 23 | uses: actions/checkout@v3 24 | 25 | - name: Setup PHP 26 | uses: shivammathur/setup-php@v2 27 | with: 28 | php-version: ${{ matrix.php }} 29 | extensions: dom, curl, libxml, mbstring, zip, pcntl, pdo, sqlite, pdo_sqlite, bcmath, soap, intl, gd, exif, iconv, imagick, fileinfo 30 | coverage: none 31 | 32 | - name: Install dependencies 33 | run: | 34 | composer require "laravel/framework:${{ matrix.laravel }}" --no-interaction --no-update 35 | composer update --${{ matrix.dependency-version }} --prefer-dist --no-interaction 36 | 37 | - name: Execute tests 38 | run: vendor/bin/phpunit 39 | -------------------------------------------------------------------------------- /composer.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "spatie/laravel-robots-middleware", 3 | "description": "Add an `all` or `none` robots header to your requests via a middleware in Laravel", 4 | "keywords": [ 5 | "robots", 6 | "spatie", 7 | "seo", 8 | "index", 9 | "robots" 10 | ], 11 | "homepage": "https://github.com/spatie/laravel-robots-middleware", 12 | "license": "MIT", 13 | "authors": [ 14 | { 15 | "name": "Sebastian De Deyne", 16 | "email": "sebastian@spatie.be", 17 | "homepage": "https://spatie.be", 18 | "role": "Developer" 19 | } 20 | ], 21 | "minimum-stability": "dev", 22 | "prefer-stable": true, 23 | "require": { 24 | "php": "^8.2", 25 | "illuminate/http": "^10|^11.0|^12.0" 26 | }, 27 | "require-dev": { 28 | "orchestra/testbench": "^8.0|^9.0|^10.0", 29 | "phpunit/phpunit": "^9.4|^10.5|^11.5.3" 30 | }, 31 | "autoload": { 32 | "psr-4": { 33 | "Spatie\\RobotsMiddleware\\": "src" 34 | } 35 | }, 36 | "autoload-dev": { 37 | "psr-4": { 38 | "Spatie\\RobotsMiddleware\\Test\\": "tests" 39 | } 40 | }, 41 | "scripts": { 42 | "test": "phpunit" 43 | } 44 | } 45 | -------------------------------------------------------------------------------- /.php_cs.dist.php: -------------------------------------------------------------------------------- 1 | in([ 5 | __DIR__ . '/src', 6 | __DIR__ . '/tests', 7 | ]) 8 | ->name('*.php') 9 | ->notName('*.blade.php') 10 | ->ignoreDotFiles(true) 11 | ->ignoreVCS(true); 12 | 13 | return (new PhpCsFixer\Config()) 14 | ->setRules([ 15 | '@PSR12' => true, 16 | 'array_syntax' => ['syntax' => 'short'], 17 | 'ordered_imports' => ['sort_algorithm' => 'alpha'], 18 | 'no_unused_imports' => true, 19 | 'not_operator_with_successor_space' => true, 20 | 'trailing_comma_in_multiline' => true, 21 | 'phpdoc_scalar' => true, 22 | 'unary_operator_spaces' => true, 23 | 'binary_operator_spaces' => true, 24 | 'blank_line_before_statement' => [ 25 | 'statements' => ['break', 'continue', 'declare', 'return', 'throw', 'try'], 26 | ], 27 | 'phpdoc_single_line_var_spacing' => true, 28 | 'phpdoc_var_without_name' => true, 29 | 'class_attributes_separation' => [ 30 | 'elements' => [ 31 | 'method' => 'one', 32 | ], 33 | ], 34 | 'method_argument_space' => [ 35 | 'on_multiline' => 'ensure_fully_multiline', 36 | 'keep_multiple_spaces_after_comma' => true, 37 | ], 38 | 'single_trait_insert_per_statement' => true, 39 | ]) 40 | ->setFinder($finder); 41 | -------------------------------------------------------------------------------- /CHANGELOG.md: -------------------------------------------------------------------------------- 1 | # Changelog 2 | 3 | All notable changes to `laravel-robots-middleware` will be documented in this file 4 | 5 | ## 1.4.1 - 2025-02-21 6 | 7 | ### What's Changed 8 | 9 | * Laravel 12.x Compatibility by @laravel-shift in https://github.com/spatie/laravel-robots-middleware/pull/26 10 | 11 | **Full Changelog**: https://github.com/spatie/laravel-robots-middleware/compare/1.4.0...1.4.1 12 | 13 | ## 1.4.0 - 2024-03-11 14 | 15 | ### What's Changed 16 | 17 | * Laravel 11.x Compatibility by @laravel-shift in https://github.com/spatie/laravel-robots-middleware/pull/25 18 | 19 | **Full Changelog**: https://github.com/spatie/laravel-robots-middleware/compare/1.3.2...1.4.0 20 | 21 | ## 1.3.2 - 2023-01-23 22 | 23 | - support Laravel 10 24 | 25 | ## 1.3.1 - 2022-01-21 26 | 27 | - support Laravel 9 28 | 29 | ## 1.3.0 - 2020-12-04 30 | 31 | - Add support for PHP 8 32 | - Drop suppoer for Laravel 5.8 33 | 34 | ## 1.2.1 - 2020-09-09 35 | 36 | - Add support for Laravel 8 37 | 38 | ## 1.2.0 - 2020-03-03 39 | 40 | - add support for Laravel 7 41 | 42 | ## 1.1.0 - 2019-09-04 43 | 44 | - add support for Laravel 6 45 | 46 | ## 1.0.3 - 2016-01-12 47 | 48 | - Fixed a bug that would occur when a `Symfony` response got returned instead of an `Illuminate` response 49 | 50 | ## 1.0.2 - 2016-01-05 51 | 52 | - Removed response typehint since it hinders redirects 53 | - Composer fix 54 | 55 | ## 1.0.1 - 2016-01-05 56 | 57 | - Moved orchestra to devdependencies 58 | 59 | ## 1.0.0 - 2016-01-05 60 | 61 | - Initial release 62 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Enable or disable the indexing of your app 2 | 3 | [![Latest Version on Packagist](https://img.shields.io/packagist/v/spatie/laravel-robots-middleware.svg?style=flat-square)](https://packagist.org/packages/spatie/laravel-robots-middleware) 4 | [![run-tests](https://github.com/spatie/laravel-robots-middleware/actions/workflows/run-tests.yml/badge.svg)](https://github.com/spatie/laravel-robots-middleware/actions/workflows/run-tests.yml) 5 | ![Check & fix styling](https://github.com/spatie/laravel-robots-middleware/workflows/Check%20&%20fix%20styling/badge.svg) 6 | [![Total Downloads](https://img.shields.io/packagist/dt/spatie/laravel-robots-middleware.svg?style=flat-square)](https://packagist.org/packages/spatie/laravel-robots-middleware) 7 | 8 | A tiny, opinionated package to enable or disable indexing your site via a middleware in Laravel. 9 | 10 | More on the Robots meta tag: https://developers.google.com/webmasters/control-crawl-index/docs/robots_meta_tag 11 | 12 | Spatie is a webdesign agency based in Antwerp, Belgium. You'll find an overview of all our open source projects [on our website](https://spatie.be/opensource). 13 | 14 | ## Support us 15 | 16 | [](https://spatie.be/github-ad-click/laravel-robots-middleware) 17 | 18 | We invest a lot of resources into creating [best in class open source packages](https://spatie.be/open-source). You can support us by [buying one of our paid products](https://spatie.be/open-source/support-us). 19 | 20 | We highly appreciate you sending us a postcard from your hometown, mentioning which of our package(s) you are using. You'll find our address on [our contact page](https://spatie.be/about-us). We publish all received postcards on [our virtual postcard wall](https://spatie.be/open-source/postcards). 21 | 22 | ## Installation 23 | 24 | You can install the package via composer: 25 | ``` bash 26 | $ composer require spatie/laravel-robots-middleware 27 | ``` 28 | 29 | ## Usage 30 | 31 | By default, the middleware enables indexing on all pages. You'll probably want to inherit your own class containing you application's indexing rule handler. 32 | 33 | ```php 34 | // app/Http/Middleware/MyRobotsMiddleware.php 35 | namespace App\Http\Middleware; 36 | 37 | use Illuminate\Http\Request; 38 | use Spatie\RobotsMiddleware\RobotsMiddleware; 39 | 40 | class MyRobotsMiddleware extends RobotsMiddleware 41 | { 42 | /** 43 | * @return string|bool 44 | */ 45 | protected function shouldIndex(Request $request) 46 | { 47 | return $request->segment(1) !== 'admin'; 48 | } 49 | } 50 | ``` 51 | 52 | Next, simply register the newly created class in your middleware stack. 53 | 54 | ```php 55 | // app/Http/Kernel.php 56 | 57 | class Kernel extends HttpKernel 58 | { 59 | protected $middleware = [ 60 | // ... 61 | \App\Http\Middleware\MyRobotsMiddleware::class, 62 | ]; 63 | 64 | // ... 65 | } 66 | ``` 67 | 68 | That's it! Responses will now always have an `x-robots-tag` in their headers, containing an `all` or `none` value. 69 | 70 | ## Changelog 71 | 72 | Please see [CHANGELOG](CHANGELOG.md) for more information what has changed recently. 73 | 74 | ## Testing 75 | 76 | ``` bash 77 | $ composer test 78 | ``` 79 | 80 | ## Contributing 81 | 82 | Please see [CONTRIBUTING](https://github.com/spatie/.github/blob/main/CONTRIBUTING.md) for details. Due to nature of this package, there's a fair chance features won't be accepted to keep it light and opinionated. 83 | 84 | ## Security 85 | 86 | If you've found a bug regarding security please mail [security@spatie.be](mailto:security@spatie.be) instead of using the issue tracker. 87 | 88 | ## Credits 89 | 90 | - [Sebastian De Deyne](https://github.com/sebastiandedeyne) 91 | - [All Contributors](../../contributors) 92 | 93 | ## License 94 | 95 | The MIT License (MIT). Please see [License File](LICENSE.md) for more information. 96 | --------------------------------------------------------------------------------