PHP code example of atldays / laravel-agent

1. Go to this page and download the library: Download atldays/laravel-agent 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/ */

    

atldays / laravel-agent example snippets


use Illuminate\Http\Request;

Route::get('/', function (Request $request) {
    $agent = $request->agent();

    return [
        'user_agent' => $agent->userAgent(),
        'hash' => $agent->hash(),
        'browser' => $agent->browser()?->name(),
        'os' => $agent->os()?->name(),
        'device' => $agent->device()?->device(),
        'is_bot' => $agent->isBot(),
    ];
});

use Atldays\Agent\Facades\Agent;
use Atldays\Agent\Facades\AgentManager;

$currentAgent = AgentManager::request();
$customAgent = AgentManager::detect($userAgent);

$browser = Agent::browser();
$os = Agent::os();
$device = Agent::device();
$bot = Agent::bot();

use Atldays\Agent\Contracts\AgentContract;

class SomeAction
{
    public function __invoke(AgentContract $agent): array
    {
        return [
            'browser' => $agent->browser()?->name(),
            'device' => $agent->device()?->device(),
            'is_bot' => $agent->isBot(),
        ];
    }
}

$agent = request()->agent();

if ($agent->isBot()) {
    // ...
}

$browser = $agent->browser();
$os = $agent->os();
$device = $agent->device();

$browser = request()->agent()->browser();

if ($browser?->isEdge()) {
    // ...
}

$os = request()->agent()->os();

if ($os?->isLinux()) {
    // ...
}

$device = request()->agent()->device();

if ($device?->isTablet()) {
    // ...
}

use Atldays\Agent\Facades\AgentManager;

$agent = AgentManager::detect($userAgent);

$browserName = $agent->browser()?->name();
$isAndroid = $agent->os()?->isAndroid();
$isMobile = $agent->device()?->isMobile();

use Atldays\Agent\AgentManager;

$manager = app(AgentManager::class);

$agent = $manager->detect($userAgent);

use Atldays\Agent\Casts\AgentCast;
use Illuminate\Database\Eloquent\Model;

class Visit extends Model
{
    protected function casts(): array
    {
        return [
            'user_agent' => AgentCast::class,
        ];
    }
}

$visit->user_agent->browser()?->name();
$visit->user_agent->device()?->isMobile();

use Atldays\Agent\Concerns\HasAgent;
use Illuminate\Database\Eloquent\Model;

class Visit extends Model
{
    use HasAgent;
}

$visit->agent()->browser()?->name();
$visit->agent()->os()?->isMacOs();
$visit->agent()->device()?->isPhone();

protected function getUserAgentColumn(): string
{
    return 'agent_string';
}

Route::middleware('agent.block-bots')->group(function () {
    // ...
});

Route::middleware('agent.block-bots:Googlebot,Bingbot')->group(function () {
    // ...
});