How to Get Browser Name and Version in Laravel

  • 18-02-2023
  • 1000
  • Laravel 10
  • Haresh Chauhan

Watch Youtube Video (HINDI) :

In this post I will tell you how To get the browser name and version in Laravel, using the agent composer package library install we can get all about your current request, composer requires jenssegers/agent which provides a simple and easy way to detect the browser, platform, and device information of the current request.

This article helps you to integrate composer how to install in laravel application as well as basic to advanced usage of the agent composer library.

By installing The "jenssegers/agent" composer library in the app, we can easy to find all details about the request. This tutorial included from scratch composer installation, configuration, and basic to advance level usage with different examples. using the laravel agent composer package, you can also define the device name, browser name, robot name, browser version, platform, etc...

Step 1. Clone Project

In this step, we will create a new fresh laravel project, if you already created then skip this step and move forward next step.

composer create-project laravel/laravel:^9.0 example-app

Step 2. Install Composer

Now we will install composer for getting the framework library file in our application, just copy and paste the below-provided command in your command prompt.

composer require jenssegers/agent

Step 3. Configuration

After successfully installing the composer, we will register the service provider and alias name in the "config/app.php" file.

config/app.php
'providers' => [
    ....
    ....
    Jenssegers\Agent\AgentServiceProvider::class,
]
'aliases' => [
    ....
    ....
    'Agent' => Jenssegers\Agent\Facades\Agent::class,
]

By adding these two configurations in the app.php file, the configuration successfully has done for the use agent in our application. now we will see basic to advance level usage, see the below example.

Step 4. Basic Usage With is() method

Basic with is() method. Using is() you can find your browser name, you need to pass the browser name inside is() method.

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

use Jenssegers\Agent\Agent;

class HomeController extends Controller
{
    public function index(Request $request)
    {
      $agent = new Agent();

      $agent->is('Windows'); // True or False
      $agent->is('Firefox'); // True or False
      $agent->is('iPhone'); // True or False
      $agent->is('OS X'); // True or False
    }
}

Step 5. Use With Magic is-method

In this method, you need to call the browser name method, as in the below example. This method is not the same as is() method.

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

use Jenssegers\Agent\Agent;

class HomeController extends Controller
{
    public function index(Request $request)
    {
      $agent = new Agent();

      $agent->isAndroidOS(); // TRUE OR False
      $agent->isNexus(); // TRUE OR False
      $agent->isSafari(); // TRUE OR False
    }
}

Step 6. Mobile detection

Using this example you can define your device whether is a mobile or tablet device.

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

use Jenssegers\Agent\Agent;

class HomeController extends Controller
{
    public function index(Request $request)
    {
      $agent = new Agent();

      $agent->isMobile(); // TRUE
      $agent->isTablet(); // FALSE
    }
}

Step 7. Regular Expression

You can also pass regular expression if there is something found like you provided expression.

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

use Jenssegers\Agent\Agent;

class HomeController extends Controller
{
    public function index(Request $request)
    {
      $agent = new Agent();

      $agent->match('regexp'); // ADD YOUR EXPRESSION IN PASS ARGUMENT
    }
}

Step 8. Device Name

You will get your device name using the device() method.

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

use Jenssegers\Agent\Agent;

class HomeController extends Controller
{
    public function index(Request $request)
    {
      $agent = new Agent();

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

Step 9. Identify Operating System

Also, you can identify your operating system, and use the platform() method to know the request system name.

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

use Jenssegers\Agent\Agent;

class HomeController extends Controller
{
    public function index(Request $request)
    {
      $agent = new Agent();

      $platform = $agent->platform();

      // WINDOWS 10
    }
}

Step 10. Robot detection

You can also define whether the coming request is a robot or human, robot() will get to know you whether the request is user-friendly or robot.

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

use Jenssegers\Agent\Agent;

class HomeController extends Controller
{
    public function index(Request $request)
    {
      $agent = new Agent();

      $agent->isRobot();

      // TRUE OR FALSE

      $robot = $agent->robot();

      // WILL GET ROBOT NAME
    }
}

Step 11. Browser Name

If you want to know your browser name, use the browser() method, to know the browser name use version() method.

platform() method will return the platform of your request as well as the version of the platform.

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

use Jenssegers\Agent\Agent;

class HomeController extends Controller
{
  public function index(Request $request)
  {
    $agent = new Agent();

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

    // GET BROWSER NAME AND VERSION

    $platform = $agent->platform();
    $version = $agent->version($platform);

    // GET PLATFORM NAME AND VERSION
  }
}

We always thanks to you for reading our blogs.


dharmesh-image

Dharmesh Chauhan

(Swapinfoway Founder)

Hello Sir, We are brothers origin from Gujarat India, Fullstack developers working together since 2016. We have lots of skills in web development in different technologies here I mention PHP, Laravel, Javascript, Vuejs, Ajax, API, Payment Gateway Integration, Database, HTML5, CSS3, and Server Administration. So you need our service Please Contact Us

haresh-image

Haresh Chauhan

(Co-Founder)


We Are Also Recommending You :