PHP code example of avvertix / laravel-agent-request

1. Go to this page and download the library: Download avvertix/laravel-agent-request library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

avvertix / laravel-agent-request example snippets


use Avvertix\AgentRequest\LaravelAgentRequest\Http\AgentRequest;
use Avvertix\AgentRequest\LaravelAgentRequest\Enums\AgentType;

class PageController
{
    public function show(AgentRequest $request)
    {
        if ($request->isAgent()) {
            // Any AI agent
        }

        $type = $request->detect();          // AgentType::CRAWLER, ::ASSISTANT, ::TOOL, or ::HUMAN
        $name = $request->agentName();       // e.g. 'GPTBot', 'Claude', null for humans

        if ($type === AgentType::ASSISTANT) {
            // Respond differently for interactive AI assistants
        }

        if ($request->expectsMarkdown()) {
            return response($markdown, headers: ['Content-Type' => 'text/markdown']);
        }
    }
}

use Avvertix\AgentRequest\LaravelAgentRequest\Http\Middleware\DenyAgentMiddleware;
use Avvertix\AgentRequest\LaravelAgentRequest\Http\Middleware\TraceAgentMiddleware;

->withMiddleware(function (Middleware $middleware) {
    $middleware->append(DenyAgentMiddleware::class);
    $middleware->append(TraceAgentMiddleware::class);
})

$request->isAgent();        // bool — any AI agent
$request->isAiAssistant();  // bool — user-facing AI assistant

use Avvertix\AgentRequest\LaravelAgentRequest\Actions\DetectAgent;

$detector = DetectAgent::fromRequest($request);

$detector->detect();      // AgentType
$detector->agentName();   // ?string
$detector->isAgent();
$detector->isAiCrawler();

use Avvertix\AgentRequest\LaravelAgentRequest\Actions\DetectAgent;

class MyDetector extends DetectAgent
{
    protected static array $aiAssistants = [
        ...parent::$aiAssistants,
        'Acme' => 'AcmeAgent',
    ];
}

// config/agent-request.php
'detector' => MyDetector::class,
bash
php artisan vendor:publish --tag="laravel-agent-request-config"
bash
# Write (or overwrite) public/robots.txt with all known agents
php artisan agent-request:robots-txt

# Limit to specific categories
php artisan agent-request:robots-txt --category=crawler
php artisan agent-request:robots-txt --category=assistant --category=crawler

# Append missing entries to an existing robots.txt without duplicating
php artisan agent-request:robots-txt --merge