Skip to content

Centum\Url

This component enables you to easily change your base URI across your codebase. It centralises URI management, making it simple to update your application URLs in one place without modifying every reference manually. This can be useful in cases where the URI might change depending on the environment (for example: development, staging, or production).

Centum\Url\Url(
    string $baseUri = ""
);

Centum\Url\Url implements Centum\Interfaces\Url\UrlInterface.

Centum\Url\Url has 2 public methods:

Centum\Url\Url takes care of trailing and leading slashes, so you do not need to worry about double slashes or missing separators:

use Centum\Url\Url;

$baseUri = "https://example.com";

$url = new Url($baseUri);

// https://example.com/path/to/something
echo $url->get("path/to/something");

// https://example.com/path/to/something
echo $url->get("/path/to/something");

You can also specify URL arguments. They are automatically sanitised and encoded by http_build_query(), which prevents errors and ensures safe URLs:

// https://example.com/search?query=hello+world&page=123
echo $url->get(
    "/search",
    [
        "query" => "hello world",
        "page"  => 123,
    ]
);

Using Centum\Url\Url helps enforce consistent URL formatting throughout your project. For example, all generated URLs will correctly handle slashes, query parameters, and encoding, reducing the chance of broken links or misformatted requests. This component can take care of environment-specific logic, such as switching between HTTP and HTTPS.