Welcome Guest, Not a member yet? Register   Sign In
Extending UserAgent class
#4
Heart 

Hello jacobs-kmi,

Here is what you need to do / can do.

1) Create a file in located at /app/Libraries/MyUserAgent.php with the following code:


Code:
<?php

namespace App\Libraries;
use \CodeIgniter\HTTP\UserAgent as BaseUserAgent;

class MyUserAgent extends BaseUserAgent {
    // Your overriding methods here
}


2) Override the service(s) that call UserAgent, such that they use your class instead.  To do this, open /app/Config/Services.php, and add this method, which was copied and modified from the system-level config:


PHP Code:
// This was copied from /vendor/codeigniter4/framework/system/Config/Services.php
public static function request(?App $config nullbool $getShared true)
{
    if ($getShared) {
        return static::getSharedInstance('request'$config);
    }

    $config $config ?? config('App');

    return new IncomingRequest(
        $config,
        AppServices::uri(),
        'php://input',
        new \App\Libraries\MyUserAgent() /// notice that this was modified to use your class
    );



Now, when you call $this->request->getUserAgent(), you'll receive an instance of your custom class.  Fuiyooooooh!
Reply


Messages In This Thread
Extending UserAgent class - by jacobs-kmi - 03-08-2022, 07:53 AM
RE: Extending UserAgent class - by Bennycopter - 03-08-2022, 10:50 AM
RE: Extending UserAgent class - by kenjis - 03-08-2022, 05:09 PM
RE: Extending UserAgent class - by Bennycopter - 03-10-2022, 08:19 AM
RE: Extending UserAgent class - by jacobs-kmi - 03-21-2022, 01:16 PM



Theme © iAndrew 2016 - Forum software by © MyBB