# Uri\_helper

This utility library offers a set of functions for managing **URLs**, **handling redirects**, **sanitizing input**, **generating slugs**, and **processing request URIs** in web applications.

## Nginx in `Uri_helper`

Since `Uri_helper` provides functions for handling request URIs and generating structured URLs, proper Nginx configuration is required to **remove `index.php` from URLs** and allow `request_uri()` to function correctly.

### **Pretty URLs**

```nginx
location / {
    try_files $uri $uri/ /index.php?$query_string;
}
```

* If the requested URL matches a file or directory, it serves it directly.
* Otherwise, the request is rewritten to `index.php`, ensuring `request_uri()` processes a **clean and structured URL**.

This ensures URLs like:

`https://phpfast.net/products?page=2` remain structured without unnecessary slashes or `index.php` in the URL.

### **Handling Redirects Efficiently**

The `redirect()` function in `Uri_helper` uses PHP’s `header('Location: ...')` method for redirection. However, on Nginx, it is possible to leverage **server-level redirection for efficiency**.

**Optimizing Redirects in Nginx**\
Instead of handling all redirects in PHP, you can configure **Nginx to process common redirects** like forcing HTTPS or redirecting old URLs:

```nginx
server {
    listen 80;
    server_name phpfast.net;
    return 301 https://$server_name$request_uri;
}
```

* Redirects HTTP to HTTPS without relying on PHP.
* Reduces the need for `redirect()` calls in the application, **improving performance**.

### Supporting Clean Request URIs

The `request_uri()` function in `Uri_helper` ensures clean URL formatting, **removing unnecessary slashes** and normalizing paths. However, Nginx needs to pass the correct `$_SERVER['REQUEST_URI']` to PHP.

**Ensure Correct Request URI Handling in Nginx**

```nginx
fastcgi_param REQUEST_URI $request_uri;
```

* Ensures `request_uri()` receives the correct request path.
* Prevents duplicate slashes (`//`) in URLs from being processed incorrectly.

Example Usage with `request_uri()` in PHP:

```php
echo request_uri('https://phpfast.net//user/info');
// Output: user/info
```

Without proper configuration, Nginx may pass incorrect paths, **leading to unexpected results** in `request_uri()`.

## Functions in Uri\_helper

### `request_uri()`

* Retrieves **the clean request URI** from `$_SERVER['REQUEST_URI']`.
* **Removes extra slashes** and normalizes the path.
* **Redirects if necessary** to enforce a clean URL structure.

Example:

```php
echo request_uri('https://phpfast.net/user/info/');
// Output: user/info/

echo request_uri('https://phpfast.net/user//info//');
// Output: user/info/

echo request_uri('https://phpfast.net/user/info?page=3');
// Output: user/info/

echo request_uri('https://phpfast.net/');
// Output: empty
```

### `base_url()`&#x20;

* **Generates the base URL** for the application.
* **Retrieves the `app_url` from the config file** if not already stored in `$base_url`.
* **Trims extra slashes and handles query strings**, ensuring a **clean and structured URL**.

```php
echo base_url('products?page=2');
// Output: https://phpfast.net/products?page=2
```

### `redirect()`

* **Redirects the user** to another URL using the `header()` function.
* `exit();` ensures that no further code is executed after redirection.

Example:

```php
redirect('https://phpfast.net/dashboard');
```

### `sanitize_url()`

* **Sanitizes URLs** by removing invalid characters.
* Uses `FILTER_SANITIZE_URL`, a built-in PHP filter.

Example:

```php
echo sanitize_url('https://phpfast.net/<script>alert(1)</script>');
// Output: https://phpfast.net/ AND alert(1)
```

### `url_slug()`

* **Converts UTF-8 strings into SEO-friendly slugs**.
* **Removes special characters**, spaces, and accents (e.g., `Đ` → `D`).
* **Replaces spaces with a delimiter (`-` by default)**.

Example:

```php
echo url_slug('New Products in 2025!');
// Output: new-products-in-2025
```

<table data-header-hidden><thead><tr><th width="295"></th><th></th></tr></thead><tbody><tr><td><strong>Function</strong></td><td><strong>Purpose</strong></td></tr><tr><td><code>base_url()</code></td><td>Generates the base URL of the application.</td></tr><tr><td><code>redirect()</code></td><td>Redirects to another URL.</td></tr><tr><td><code>sanitize_url()</code></td><td>Cleans a URL from invalid characters.</td></tr><tr><td><code>url_slug()</code></td><td>Converts a UTF-8 string into a slug.</td></tr><tr><td><code>request_uri()</code></td><td>Retrieves the clean request URI.</td></tr></tbody></table>


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://docs.cmsfullform.com/documents/helpers/uri_helper.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
