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
4
Answers
Just use isset function to check either the segment is exists or not. Like bellow:
That’s it!
you can use this to check availability of your segement request
if you still confuse you can read this documentation :
https://codeigniter.com/user_guide/libraries/uri.html#creating-uri-instances
Solution
Disable Throwing Exceptions
Hence if your sample URI is
http://localhost:8080/segment1/
:Extra tip:
best way to handle local and server segment issue need to change getSegment function
getSegment function you can find system/http/URI.php
getSegment function will look like this by default
need to change segment number like