Codeigniter 4 Handle Request URI Segment If the URL is empty

I am using Codeigniter 4 framework and I have a function for getting the URI segment like

    $bahasa = $request->uri->getSegment(1);
    $url = $request->uri->getSegment(2);
    $content = $request->uri->getSegment(3);

the code is working if the URL most like:
http://localhost:8080/segment1/segment2/segment3
but if I put the URL like http://localhost:8080/segment1/ the code is an error, the error is
CodeIgniterHTTPExceptionsHTTPException Request URI segment is out of range: 2