User Agent Class Concept in CodeIgniter 4 Tutorial

Share this Article
Reading Time: 4 minutes
485 Views

User Agent Class is used to capture the user device identity like browser used, operating system used, mobile type etc. In CodeIgniter 4, we have inbuilt class for this. The User Agent class is always available directly from the current IncomingRequest instance. We will see in this article.

By default there are several user agents configured inside codeigniter 4 application.

Inside this article we will see the complete concept of User Agent Class concept in CodeIgniter 4.

Learn More –

Note*: For this article, CodeIgniter v4.1 setup has been installed. May be when you are seeing, version will be updated. CodeIgniter 4.x still is in development mode.

Let’s get started.


Download & Install CodeIgniter 4 Setup

We need to download & install CodeIgniter 4 application setup to system. To set application we have multiple options to proceed.

Here are the following ways to download and install CodeIgniter 4 –

  • Manual Download
  • Composer Installation
  • Clone Github repository of CodeIgniter 4

Complete introduction of CodeIgniter 4 basics – Click here to go. After going through this article you can easily download & install setup.

Here is the command to install via composer –

$ composer create-project codeigniter4/appstarter codeigniter-4

Assuming you have successfully installed application into your local system.


What is User Agent Class?

The User Agent Class provides functions that help identify information about the browser, mobile device, or robot visiting your site.

How to User Agent Class?

The User Agent class is always available directly from the current IncomingRequest instance. By default, you will have a request instance in your controller that you can retrieve the User Agent class from:

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

User Agent Definition & Usage

Open UserAgents.php from /app/Config folder.

Inside this class file, we can find several type pre defined user agents. Also if we want to add in a custom way, then also we can add into this class file.

Let’s see How to use it in application?

$ php spark make:controller Site --suffix

It will create a file called SiteController.php inside /app/Controllers

<?php

namespace App\Controllers;

class SiteController extends BaseController
{
	public function checkAgent()
	{
		$agent = $this->request->getUserAgent();

		if ($agent->isBrowser()) {
			$currentAgent = $agent->getBrowser() . ' ' . $agent->getVersion();
		} elseif ($agent->isRobot()) {
			$currentAgent = $this->agent->robot();
		} elseif ($agent->isMobile()) {
			$currentAgent = $agent->getMobile();
		} else {
			$currentAgent = 'Unidentified User Agent';
		}

		echo $currentAgent;
	}

	public function checkBrowser()
	{
		$agent = $this->request->getUserAgent();

		if ($agent->isBrowser('Safari')) {

			echo 'You are using Safari.';
		} elseif ($agent->isBrowser()) {

			echo 'You are using a browser.';
		}
	}

	public function checkMobileType()
	{
		$agent = $this->request->getUserAgent();

		if ($agent->isMobile('iphone')) {
			// .. code here
		} elseif ($agent->isMobile("blackberry")) {
			// .. code here
		} else {
			// .. code here
		}
	}
}

We hope this article helped you to learn User Agent Class Concept in CodeIgniter 4 Tutorial in a very detailed way.

If you liked this article, then please subscribe to our YouTube Channel for PHP & it’s framework, WordPress, Node Js video tutorials. You can also find us on Twitter and Facebook.

Find More on CodeIgniter 4 here

Leave a Comment