Welcome Guest, Not a member yet? Register   Sign In
Bug (?) by using ResponseTrait within a filter
#1

(This post was last modified: 10-15-2022, 06:14 AM by groovebird. Edit Reason: It is not a bug - adjust title )

Hi,

i want to use the ResponseTrait in a filter but i get the following error:

Undefined property: App\\Filters\\BackendAuth::$response"

This is my filter:

PHP Code:
namespace App\Filters;

use 
CodeIgniter\Filters\FilterInterface;
use 
CodeIgniter\HTTP\RequestInterface;
use 
CodeIgniter\HTTP\RequestTrait;
use 
CodeIgniter\HTTP\ResponseInterface;
use 
CodeIgniter\API\ResponseTrait;

class 
BackendAuth implements FilterInterface {
    use ResponseTrait;
    public function before(RequestInterface $request$arguments null)
    {
        
        
// Do something here
    }

    public function after(RequestInterface $requestResponseInterface $response$arguments null)
    {
        
        
return $this->failUnauthorized();
    }

Reply
#2

ResponseTrait is not intended to use with filters.
It is for controllers.
Reply
#3

(10-14-2022, 04:51 PM)kenjis Wrote: ResponseTrait is not intended to use with filters.
It is for controllers.

Ok, thank you.
I want to check globally in my controller if access is allowed or not. Thats why i tried at first to use the Response Trait in the constructor of my controller but this is not working.

Is it right, that some things, if used in the constructor, not work as expected? The Response Trait is one thing and the setHeader method does not set the header if i use it in the constructor.
Reply
#4

(10-15-2022, 04:16 AM)groovebird Wrote: Is it right, that some things, if used in the constructor, not work as expected?

It does not work as expected.
Because the constructor cannot return a Response object.
Controller should return a Response object.

And in a filter, what you do is just return the Response object that you want.
See https://codeigniter4.github.io/CodeIgnit...ponse.html
Reply
#5

(10-15-2022, 05:37 AM)kenjis Wrote: It does not work as expected.
Because the constructor cannot return a Response object.
Controller should return a Response object.

And in a filter, what you do is just return the Response object that you want.
See https://codeigniter4.github.io/CodeIgnit...ponse.html

@kenjis thanks for the clarification. Then the correct way is to use a filter for things (not only the access check) i want to execute every time the controller is executed?
Reply




Theme © iAndrew 2016 - Forum software by © MyBB