The [Agent] package seems to be incompatible with Laravel 11. Let's move forward with a different approach to achieve the desired functionality.
Warning: This package is for [demonstration purposes / personal learning only]. Use at your own risk! It may have bugs or functionalities not suitable for production.
In Laravel versions below 11, you'll need to use Agent recommended.
allowing you to make informed decisions about how to present your content and interact with your users
based on the device they're using. This results in a more tailored and user-friendly experience for everyone.
- "Another package relies on the features provided by package-name above. We need to ensure it's included as a dependency."
Install using composer:
composer require spinzar/laravel-agent
Add the service provider in config/app.php
:
Spinzar\LaravelAgent\AgentServiceProvider::class,
And add the LaravelAgent alias to config/app.php
:
'LaravelAgent' => Spinzar\LaravelAgent\Facades\LaravelAgent::class,
Start by creating an LaravelAgent
instance (or use the LaravelAgent
Facade if you are using Laravel):
use Spinzar\LaravelAgent\LaravelAgent;
$larvaelagent = new LaravelAgent();
If you want to parse user agents other than the current request in CLI scripts for example, you can use the setUserAgent
and setHttpHeaders
methods:
$larvaelagent->setUserAgent('Mozilla/5.0 (Macintosh; Intel Mac OS X 10_6_8) AppleWebKit/537.13+ (KHTML, like Gecko) Version/5.1.7 Safari/534.57.2');
$larvaelagent->setHttpHeaders($headers);
Check for a certain property in the user agent.
$larvaelagent->is('Windows');
$larvaelagent->is('Firefox');
$larvaelagent->is('iPhone');
$larvaelagent->is('iPad');
$larvaelagent->is('OS X');
Magic method that does the same as the previous is()
method:
$larvaelagent->isAndroidOS();
$larvaelagent->isNexus();
$larvaelagent->isSafari();
Check for mobile device:
$larvaelagent->isMobile();
$larvaelagent->isTablet();
Search the user agent with a regular expression:
$larvaelagent->match('regexp');
Get the browser's accept languages. Example:
$languages = $larvaelagent->languages();
// ['nl-nl', 'nl', 'en-us', 'en']
Get the device name, if mobile. (iPhone, Nexus, AsusTablet, ...)
$device = $larvaelagent->device();
Get the operating system. (Ubuntu, Windows, OS X, ...)
$platform = $larvaelagent->platform();
Get the browser name. (Chrome, IE, Safari, Firefox, ...)
$browser = $larvaelagent->browser();
Check if the user is using a desktop device.
$larvaelagent->isDesktop();
This checks if a user is not a mobile device, tablet or robot.
Check if the user is using a phone device.
$larvaelagent->isPhone();
Check if the user is a robot. This uses jaybizzle/crawler-detect to do the actual robot detection.
$larvaelagent->isRobot();
Get the robot name.
$robot = $larvaelagent->robot();
MobileDetect recently added a version
method that can get the version number for components. To get the browser or platform version you can use:
$browser = $larvaelagent->browser();
$version = $larvaelagent->version($browser);
$platform = $larvaelagent->platform();
$version = $larvaelagent->version($platform);
Note, the version method is still in beta, so it might not return the correct result.
Laravel User Agent is licensed under The MIT License (MIT).