🔥 CodeIgniter Intermediate

What is the URI class in CodeIgniter 4?

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.