What is the URI class in CodeIgniter 4?
Why Interviewers Ask This
This question targets practical, hands-on experience with CodeIgniter. Interviewers want to see if you've worked with these concepts in real projects, not just read about them. Strong answers include concrete examples.
Answer
The URI class in CI4 provides methods for working with the current URL. Access via the request object: $uri = service("request")->getUri() or current_url(true). Get URI segments: $uri->getSegment(1) (first segment after the base URL). Get all segments: $uri->getSegments(). Get the path: $uri->getPath(). Get query string: $uri->getQuery(). Get a specific query param: $uri->getQueryParams()["page"]. Build URLs: $uri = new \CodeIgniter\HTTP\URI("https://example.com"); $uri->addQuery("page", 2). The URI class supports the PSR-7 URI interface methods. Helper functions: uri_string() returns the path portion of the current URL, current_url() returns the full current URL, base_url() returns the base URL, segment($n) returns the nth URL segment.
Pro Tip
Demonstrate both theoretical understanding and practical experience. Say what it is, then give an example of how you actually used it in a CodeIgniter codebase.