Welcome Guest, Not a member yet? Register   Sign In
RequestInterface - missing methods
#1

Hi,

this is from the documentation:

"The User Agent class is always available directly from the current IncomingRequest instance."

PHP Code:
<?php

use CodeIgniter\HTTP\RequestInterface;

class 
SomeClass
{
    protected 
$request;

    public function 
__construct(RequestInterface $request)
    {
        
$this->request $request;
    }


$this->request->getUserAgent() and other methods are not available. Is this a bug in the documentation? The RequestInterface has no such methods.
Reply
#2

(This post was last modified: 06-22-2022, 02:25 PM by kenjis.)

IncomingRequest is not RequestInterface.
Reply
#3

(06-22-2022, 02:25 PM)kenjis Wrote: IncomingRequest is not RequestInterface.

I know, but this is the code in the documentation and it is not working for all cases. With this code i have no access to the userAgent and other methods. It is only working if i use the IncomingRequest $request, but this is different from the documentation.
Reply
#4

(This post was last modified: 06-22-2022, 02:35 PM by kenjis.)

(06-22-2022, 02:33 PM)groovebird Wrote: I know, but this is the code in the documentation and it is not working for all cases. With this code i have no access to the userAgent and other methods. It is only working if i use the IncomingRequest $request, but this is different from the documentation.

Where in the documentation?

If it is in the documentation, it is a bug of the documentation.
Reply
#5

(06-22-2022, 02:35 PM)kenjis Wrote: Where in the documentation?

If it is in the documentation, it is a bug of the documentation.

This is the first Link:
https://codeigniter.com/user_guide/libra...-the-class

"The User Agent class is always available directly from the current IncomingRequest instance."

There is a link to this page: https://codeigniter.com/user_guide/incom...quest.html

It’s preferable, though, to pass the request in as a dependency if the class is anything other than the controller, where you can save it as a class property:

with the code i posted earlier:

PHP Code:
<?php

use CodeIgniter\HTTP\RequestInterface;

class 
SomeClass
{
    protected 
$request;

    public function 
__construct(RequestInterface $request)
    {
        
$this->request $request;
    }
}

$someClass = new SomeClass(\Config\Services::request()); 

Here the RequestInterface is used but there is no UserAgent object
Reply
#6

(This post was last modified: 06-22-2022, 03:23 PM by kenjis.)

Thanks.

Quote:It’s preferable, though, to pass the request in as a dependency if the class is anything other than the controller, where you can save it as a class property:

This is correct, but RequestInterface is not IncomingRequest.
You can use RequestInterface, but if you use it, you can't use some methods in IncomingRequest.

It is better to fix the sample code.
Reply




Theme © iAndrew 2016 - Forum software by © MyBB