codeigniter 4 - Loading helpers and user_agent library in model

Viewed 127

I've been struggle with this for hours. I would like to use helpers and the user_agent library on a model.

  1. User_agent: every-time I'm trying to load the user agent library using the next code:

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

I getting the next error:

Error
Call to a member function getUserAgent() on null
  1. When trying to load helper and use it from model for example cookie helper:

helper(cookie);

I think it's loading the helper but when trying to use it I printing next error:

Error Call to a member function is_mobile() on null

1 Answers

There isn't any need to load helpers.

User Agent Class

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

$agent = \Config\Services::request()->getUserAgent();
$isMobile = $agent->isMobile();
Related