• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Need a little more info using Filters 'After'

#1
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?
Reply

#2
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.
Support Development  • Practical CodeIgniter 3  • Vulcan - CLI Tools for CI4
Reply

#3
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?
}
Reply

#4
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);

Support Development  • Practical CodeIgniter 3  • Vulcan - CLI Tools for CI4
Reply

#5
Boom!  That's what I needed.  Thank you!
Reply


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2019 MyBB Group.