Laravel detector mobile browser name version platform device robot crawler user language

Panjeh
5 min readFeb 21, 2019

--

This package is used in Laravel Jetstream.

Before you continue with the rest of this tutorial, I would like to introduce two packages for Laravel that I have recently developed: Laravel Pay Pocket, a modern multi-wallet package, and Laravel Failed Jobs, a UI for the Laravel Failed Jobs Table. I hope they may be of help to you.

https://github.com/HPWebdeveloper/laravel-pay-pocket
https://github.com/HPWebdeveloper/laravel-failed-jobs

Introduction:

The most reputable Laravel package provided for detecting user agent is Agent developed by Jens Segers. All the information you need to know about user are accessible by using Agent (jenssegers/agent) package. Agent utilizes the Mobile Detect PHP Class under the hood. Agent makes Laravel equipped to detect the user environment, i.e:

1- Detecting user platform and its version: Including Windows, Ubuntu, OS X, AndroidOS, etc.

2- Detecting user device and device name: Including mobile, tablet, desktop and it will get its name (iPhone, Nexus, AsusTablet, …).

3- Detecting user browser and its version: Including Firefox, Chrome, Safari, IE, etc.

4- And also detecting user language.

How to implement:

First of all do this:

composer require jenssegers/agent

Then find the config/app.php and then find providers key as below:

Then add this line as the Agent service provider to the array of providers:

Jenssegers\Agent\AgentServiceProvider::class,

Then in config/app.php find the aliases key as below:

add an alias for Agent to the array of aliases like:

'Agent' => Jenssegers\Agent\Facades\Agent::class,

It’s done!

How to use:

First, put this line at the top of the controller:

use Jenssegers\Agent\Agent;

Then start by creating an Agent instance (or use the Agent Facade).

$agent = new Agent();

Useful methods:

$agent->platform();
// Ubuntu, Windows, OS X, ...
$agent->browser();
// Chrome, IE, Safari, Firefox, ...
$browser = $agent->browser();
$version = $agent->version($browser);
$platform = $agent->platform();
$version = $agent->version($platform);

and more:

$agent->is('Windows');
$agent->is('Firefox');
$agent->is('iPhone');
$agent->is('OS X');
$agent->isAndroidOS();
$agent->isNexus();
$agent->isSafari();
$agent->languages();
// ['nl-nl', 'nl', 'en-us', 'en']
$agent->device();
// iPhone, Nexus, AsusTablet, ...
$agent->isDesktop();
$agent->isPhone();
$agent->isMobile();
$agent->isTablet();
$agent->isRobot();$agent->robot();
// robot name

More examples and original methods can be find here:

https://github.com/serbanghita/Mobile-Detect/wiki/Code-examples

Blade view file usage:

If you pass the $agent to a specific view blade file you can do this:

@if ($agent->isMobile())

Show content for mobile ...

@endif

Global Blade view file usage:

Moreover you can share $agent and make it available inside all of your view blade files in this way:

Make AgentServiceProvider.php in app/providers/ directory by :

php artisan make:provider AgentServiceProvider

Then register this provider (the below line) in config/app.php file in the array of providers as described before:

App\Providers\AgentServiceProvider::class,

Then fill the AgentServiceProvider.php with:

namespace App\Providers;

use View;
use Jenssegers\Agent\Agent;
use Illuminate\Support\ServiceProvider;

class AgentServiceProvider extends ServiceProvider
{
public function boot()
{
$agent = new Agent();

View::share('agent', $agent);
}

public function register()
{
//
}
}

Then inside the view blade file use this:

@if ($agent->isMobile())

Show content for mobile ...
@endif

Some other useful packages:

1- https://github.com/riverskies/laravel-mobile-detect

2- https://github.com/serbanghita/Mobile-Detect

3- https://github.com/hisorange/browser-detect

4- https://github.com/vluzrmos/laravel-language-detector

5- https://github.com/JayBizzle/Laravel-Crawler-Detect

6- https://github.com/JayBizzle/Crawler-Detect

7- http://mobiledetect.net/

Thank you for reading! If you enjoyed this article:

Clap it ! Share it! Follow Me in Medium!

Also I’d like to hear your opinion on this article. If you have any doubt, question or suggestion please leave a comment below.

Have a very wonderful day!

Previous Stories You will Love:

--

--