Milk Admin

Route Class Documentation

This documentation provides a detailed overview of the Route class and its functions.

Introduction

The Route class manages page routing within the system. It allows registering and executing specific functions for pages, building URLs, and handling redirects.

Static Variables

  • $functions: Array of registered functions for page routing.

Functions

set($name, $function, $permission = null)

Registers a function to be called when the page starts.


Route::set('home', function() {
echo 'Welcome to the homepage!';
}, '_user.is_authenticated');
    

run($name)

Executes the registered function for the page.


Route::run('home');
    

url($query = '')

Returns the site URL with the specified query string.


$url = Route::url(['page' => 'home']);
echo $url;
    

currentUrl()

Returns the current URL.

$current_url = Route::currentUrl();
echo $current_url;
    

redirect($query = '', $data = [])

Performs a redirect to another page.

$data is an associative array that can contain data to pass to the destination page.


Route::redirect(['page' => 'home']);
    

getHeaderData()

Retrieves the data passed in the data.

redirectSuccess($url, $message = '')

Performs a redirect with a success message.

Route::redirectSuccess('home', 'Operation completed successfully!');

redirectError($url, $message = '')

Performs a redirect with an error message.

Route::redirectError('home', 'An error occurred!');

compareQueryUrl($query1, $query2 = []) : bool

Compares the current URL's query string with the specified one.


$selected = (Route::compareQueryUrl('page=home') ? 'selected' : '');
    

parseQueryString($query_string)

Parses the current query string and returns an associative array.


$query = Route::parseQueryString('page=home&lang=it');
print_r($query);
    

getQueryString()

Returns the current query string.


$query_string = Route::getQueryString();
echo $query_string;
    

comparePageUrl($query1, $query2 = [])

Verifies that the page query between query1 and query2 are equal.

To check if the sidebar menu is active, simply use this method.

$query1 and $query2 can be strings or arrays. If $query2 is not specified, the current query is used.


$isActive = Route::comparePageUrl(['page' => 'home']);
// or
$isActive = Route::comparePageUrl('?page=home&action=foo');
    

buildQuery($query = '')

Builds the query string for the URL. Can be an array or a string.


$query_string = Route::buildQuery(['page' => 'home']);
echo $query_string;
    

urlsafeB64Encode(string $input)

Encodes a string in Base64 in a URL-safe manner.


$encoded = Route::urlsafeB64Encode('test string');
echo $encoded;
    

urlsafeB64Decode(string $input)

Decodes a URL-safe Base64 string.


$decoded = Route::urlsafeB64Decode('dGVzdCBzdHJpbmc');
echo $decoded;
    

redirectSuccess($url, $message = '')

Performs a redirect with a success message.


Route::redirectSuccess('home', 'Operation completed successfully!');
    

redirectError($url, $message = '')

Performs a redirect with an error message.


Route::redirectError('home', 'An error occurred!');
    

getHeaderData()

Retrieves data from headers.


$data = Route::getHeaderData();
print_r($data);
    

extractCredentials($username_key = 'username', $password_key = 'password')

Extracts authentication credentials from the HTTP request. Handles different authentication methods: Basic Auth, POST, and JSON body.

Parameters:

  • $username_key: Custom key for the username field (default: 'username')
  • $password_key: Custom key for the password field (default: 'password')

Returns an associative array with 'username' and 'password', or empty values if not found.


$credentials = Route::extractCredentials();
print_r($credentials);

// With custom keys
$credentials = Route::extractCredentials('user', 'pass');
print_r($credentials);
    

getBearerToken()

Retrieves the Bearer token from the Authorization header.

Searches for the Bearer token in the following headers:

  • Authorization
  • HTTP_AUTHORIZATION
  • apache_request_headers() (if available)

Returns the Bearer token if found, otherwise false.


$token = Route::getBearerToken();
if ($token !== false) {
    echo "Bearer token found: " . $token;
} else {
    echo "No Bearer token found";
}
    

replaceUrlPlaceholders($url, $values = [])

Replaces placeholders in URL query parameters with actual values. Placeholders use the format %placeholder_name%.

Parameters with unmatched placeholders are removed from the query string.


$url = Route::replaceUrlPlaceholders("?page=view&id=%id%", ['id' => 12]);
// Result: "?page=view&id=12"

$url = Route::replaceUrlPlaceholders("?page=view&id=%id%", []);
// Result: "?page=view" (unmatched placeholder removed)
    
Loading...