CodeIgniter 4 using Request object in view

It can be useful to use the request object in a view to retrieve input, this was easy with CodeIgniter 3 but with CodeIgniter 4 the documentation doesn’t cover this.

Add this to the top of your view

<?php
$request = \Config\Services::request();
?>

Then you can use the request methods like this

<input type="hidden" id="score" name="score" value="<?=$request->getVar('score')?>">

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.