CodeIgniter Forums
Need a little more info using Filters 'After' - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: CodeIgniter 4 (https://forum.codeigniter.com/forum-28.html)
+--- Forum: CodeIgniter 4 Discussion (https://forum.codeigniter.com/forum-31.html)
+--- Thread: Need a little more info using Filters 'After' (/thread-74857.html)



Need a little more info using Filters 'After' - next2heaven - 11-15-2019

How do I use the $response (ResponseInterface) in the 'after' function for Filters? What are the properties that can be changed here? I need to modify the output. Also, is it possible to view controller variables in the 'after' function?


RE: Need a little more info using Filters 'After' - kilishan - 11-15-2019

You are given the Response object that contains everything that will be sent to the client, and that's it. So - no controller variables. But you do have the status code, headers, cookies, and the generated HTML.

Take that info, play with it however you need, and return the response object. Your modified response is what is returned to the client at the point.


RE: Need a little more info using Filters 'After' - next2heaven - 11-15-2019

How do I do that in the 'after' function though?  How do I update the $response to edit the body?  Do I use a 'return' statement?  Call a function on the $response object?  I can't seem to find an example anywhere of how this would work for here:

Code:
public function after(RequestInterface $request, ResponseInterface $response)
{
  // how do I manipulate the $response var?
  // do I need to return the $response var or anything?
}



RE: Need a little more info using Filters 'After' - kilishan - 11-15-2019

A couple of things to help out:

1. Docs for the Response object
2. the Response class itself

PHP Code:
public function after(RequestInterface $requestResponseInterface $response)
{
   
$body $response->getBody();
    
// Do stuff to body, set headers, whatever.
    
return $response->setBody($body);




RE: Need a little more info using Filters 'After' - next2heaven - 11-18-2019

Boom!  That's what I needed.  Thank you!