# URI Routing

## What is URI Routing?

URI Routing maps a URI to a specific controller method, determining how requests are processed.

CodeIgniter supports two types of routing:

1. Defined Route Routing – Allows manual route definition, offering full control and flexibility over URL structures.
2. Auto Routing – Automatically maps HTTP requests to the appropriate controller methods based on naming conventions, eliminating the need for manual route setup.

## `Api.php` and `Web.php`

In a web application, separating routes for APIs and web interfaces is a best practice for managing and maintaining the codebase. Below is the distinction between [**`Api.php`**](https://docs.cmsfullform.com/documents/uri-routing/api.php) and [**`Web.php`**](https://docs.cmsfullform.com/documents/uri-routing/web.php), as well as when to use each file.

## Defining Routing Rules in PHPFast

In PHPFast, routes are defined using the `$routes` object within dedicated route files. The syntax is straightforward and supports multiple HTTP methods, including GET, POST, PUT, DELETE, and more.

#### Examples

```php
$routes->get('users/', 'UsersController::list');
$routes->post('users/create', 'UsersController::create');
$routes->get('users/(:num)', 'UsersController::view:$1');
$routes->put('users/edit/(:num)', 'UsersController::edit:$1');
$routes->delete('users/delete/(:num)', 'UsersController::delete:$1');
$routes->get('admin', 'AdminController::index', [\App\Middleware\AuthMiddleware::class]);
```

### Understanding Route Structure

A route consists of two main parts:

* Route Path → The URL pattern (relative to the `BaseURL`).
* Route Handler → The Controller and its corresponding method that should process the request.

**Example:**

```php
$routes->get('/', 'HomeController::index');
```

This means that when a user visits `/` (the homepage), CodeIgniter will call the `index()` method inside the `HomeController`.

### Defining Routes with HTTP Methods

When specifying a route, you also define the HTTP method it should respond to:

* GET Request:

  ```php
  $routes->get('users', 'UsersController::list');
  ```

  * Maps `/users` to the `list()` method inside the `Users` controller.
* GET Request with Parameters:

  ```php
  $routes->get('users/1/23', 'UsersController::list/1/23');
  ```

  * Calls `$UsersController->list(1, 23)`, passing `1` and `23` as parameters.
* POST Request:

  ```php
  $routes->post('submit-form', 'UsersController::submitForm')
  ```

  * Handles form submissions via the `submitForm()` method in `UsersController`.

### Using Placeholders for Dynamic URLs

PHPFast supports placeholders to create flexible routes:

| **Placeholder**   | **Description**                                       | **Regex Equivalent** |
| ----------------- | ----------------------------------------------------- | -------------------- |
| **`(:any)`**      | Matches any characters except `/`.                    | `(.+)`               |
| **`(:segment)`**  | Matches any single segment of the URL, excluding `/`. | `([^/]+)`            |
| **`(:num)`**      | Matches only numeric values.                          | `(\d+)`              |
| **`(:alpha)`**    | Matches only alphabetic characters (A-Z, a-z).        | `([a-zA-Z]+)`        |
| **`(:alphanum)`** | Matches alphanumeric characters (A-Z, a-z, 0-9).      | `([a-zA-Z0-9]+)`     |
| **`(:hash)`**     | Matches a 32-character hexadecimal hash value.        | (\[a-fA-F0-9]{32})   |

Example:

#### 1. `(:any)` - Matches **any string** (except `/`).

```php
$routes->get('users/(:any)', 'UsersController::detail/$1');
```

**Example URL:** `/users/detail`\
**Calls:** UsersController`::view('detail')`

#### 2. `(:segment)` – Matches a single URL segment (excluding /).

```php
$routes->get('users/(:segment)', 'UsersController::show/$1');
```

**Example URL:** `/users/all`\
**Calls:** UsersController`::show('all')`

#### 3. <kbd>(:num)</kbd> - Matches only numeric values.

```php
$routes->get('users/(:num)', 'UsersCotroller::details/$1');
```

**Example URL:** `/users/123`\
**Calls:** UserController`::details(123)`

#### 4. `(:alpha)` – Matches only alphabetic characters (A-Z, a-z).

```php
$routes->get('users/(:alpha)', 'UsersController::profile/$1');
```

**Example URL:** `/user/php_fast`\
**Calls:** `UsersController::profile('php_fast')`

#### 5. <kbd>(:alphanum)</kbd> – Matches **alphanumeric characters (A-Z, a-z, 0-9)**.

```php
$routes->get('users/(:alphanum)', 'UsersController::execute/$1');
```

**Example URL:** `/users/abc123`\
**Calls:** UsersController`::execute('abc123')`

#### 6. <kbd>(:hash)</kbd> – Matches **a 32-character hexadecimal hash value**.

```php
$routes->get('verify/(:hash)', 'AuthController::verify/$1');
```

**Example URL:** `/verify/a94a8fe5ccb19ba61c4c0873d391e987`\
**Calls:** `AuthController::verify('a94a8fe5ccb19ba61c4c0873d391e987')`

### Global Options

The given lines define different types of routes in PHPFast using `$routes`. Each method corresponds to a specific HTTP request type, which determines how the route processes incoming requests.

```php
// Route that only accepts GET requests
$routes->get('product/(:num)/(:string)', 'ProductController::show:$1:$2');		

// Route that only accepts POST requests (for form submissions, data creation)
$routes->post('product/create', 'ProductController::create'); 		

// Route that only accepts PUT requests (used for updating resources)
$routes->put('product/update/(:num)', 'ProductController::update/$1');			

// Route that only accepts DELETE requests (used to delete resources)
$routes->delete('product/delete/(:num)', 'ProductController::delete/$1'); 		
			
// Applying middleware to a single route
$routes->get('admin', 'AdminController::index', [\App\Middleware\AuthMiddleware::class]);
```
