CodeIgniter Forums
Setting response outside controller - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: CodeIgniter 4 (https://forum.codeigniter.com/forumdisplay.php?fid=28)
+--- Forum: CodeIgniter 4 Discussion (https://forum.codeigniter.com/forumdisplay.php?fid=31)
+--- Thread: Setting response outside controller (/showthread.php?tid=75359)



Setting response outside controller - maspai - 01-29-2020

In CI3 I usually use something like:
PHP Code:
$ci =& get_instance();
$ci->$output->set_output($response)->_display();
exit; 

How to do it in CI4?


RE: Setting response outside controller - InsiteFX - 01-30-2020

From: CodeIgniter User Guide - Accessing the Request

If you are not within a controller, but still need access to the application’s Request object, you can get a copy of it through the Services class:


PHP Code:
$request = \Config\Services::request(); 

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: