Welcome Guest, Not a member yet? Register   Sign In
setHeader not available in Responseinterface
#1

(This post was last modified: 06-29-2022, 08:31 AM by groovebird.)

Hi,

in the BaseController there is a response property i want wo use. Like in the documentation we can use $this->response->setHeader, but there is no function setHeader. How can i set a header?

This is not working: https://codeigniter4.github.io/userguide...ng-headers
Reply
#2

(This post was last modified: 06-29-2022, 08:54 AM by iRedds. Edit Reason: harsh reality )

The Response class has a setHeader() method. It inherits from the Message class.
This is CI interface hell.
Relax and enjoy)
Reply
#3

(This post was last modified: 06-29-2022, 09:07 AM by groovebird.)

Yes the Response class, but the interface has not the needed methods. Why is it in the documentation if it is not working?
In the BaseController $response is available.

\Config\Service::response()->setHeader() is working, but if we have the response property available this makes no sense.

What is the right way? Why are the needed methods not in the interface?
Reply
#4

$this->response refers to the same instance class as \Config\Service::response()
So if \Config\Service::response()->setHeader() works, then $this->request->setHeader() will work.

I don't know why these methods are not in the interface. That is the way.
Reply
#5

@iRedds thank you.
Ok, but i don't understand why the documentation shows code which is not working. In the link above it is clearly said that we can use the response property from the base controller.
Reply
#6

I'm not sure.
But Responseinterface will be PSR-7's  ResponseInterface in the future
and it does not have setHeader() method.
So setHeader() will not be added.

The sample code works, because CI injects a Response object in $this->response.
Reply
#7

(06-29-2022, 03:35 PM)kenjis Wrote: The sample code works, because CI injects a Response object in $this->response.

I disagree, this is not true. The code ist not working and the documentation should be adjusted. CI injects the interface and the interface has no method setHeader.

\Config\Service::response()->setHeader() is working (I think this is unnecessary, because we have an response object available, but with missing setHeader())

$this->response->setHeader() is not working

The documentation at https://codeigniter4.github.io/userguide...ng-headers should show an alternative way. I think the interfaces with the missing methods makes simple things more complicated.
Reply
#8

Show your code in the BaseController that is not working.
What did you Try? What did you Get? What did you Expect?

Joined CodeIgniter Community 2009.  ( Skype: insitfx )
Reply
#9

(This post was last modified: 06-30-2022, 06:14 AM by groovebird.)

My IDE shows me this message:

https://ibb.co/0ZBtCJg

but in the browser it is working. What is the reason? The interface does not have this method
Reply
#10

https://ibb.co/CHwnGrZ =)
Reply




Theme © iAndrew 2016 - Forum software by © MyBB