FastSitePHP\AppMin

The AppMin Class contains core code from the Application Class and is much smaller in size. If you have a minimal site such as a few simple web services then AppMin could be used as an alternative to the Application Class.

Due to its small size the AppMin Class may run twice as fast as the Application Class on some servers, however this is typically a very small number (thousands or tens of thousands of a second only). In general if using PHP 7 with common production settings on a Linux Server there will be no difference between this Class and the Application Class.

If you are using this class with only a few classes you might want to consider copying the files to your project and modifying this class to fit the needs of your site.

Source Code

GitHub

رمز المثال

<?php
// Only two files are required to run FastSitePHP AppMin and they can
// be in the same directory as [index.php] or the main php page.
require 'AppMin.php';
require 'Route.php';

// Create the AppMin Object and optionally setup
// Error Handling and a Timezone.
$app = new FastSitePHP\AppMin();
$app->setup('UTC');

// Define the 'Hello World' default route
$app->get('/', function() {
    return 'Hello World!';
});

// Return a JSON Response by returning an Object or an Array
$app->get('/json', function() {
    return ['Hello' => 'World'];
});

// Send a Plain Text Response and Custom Header. AppMin is minimal in size so
// optional URL parameters [:name?] and Wildcard URL's [*] are not supported.
$app->get('/hello/:name', function($name) use ($app) {
    $app->headers = [
        'Content-Type' => 'text/plain',
        'X-Custom-Header' => $name,
    ];
    return 'Hello ' . $name;
});

// Run the App
$app->run();

Properties

Name Data Type Default Description
status_code int
null
null HTTP Response Status Code
headers array [] HTTP Response Headers
cors_headers array
null
null HTTP Response Headers for CORS
no_cache bool false If [true] then the following response headers will be sent to the client:

    Cache-Control: no-cache, no-store, must-revalidate
    Pragma: no-cache
    Expires: -1
template_dir string
null
null Optional Location of the template files that get rendered when using the [render()] function.
error_template string
array
null
null Array of template files or a single file name as a string.
not_found_template string
array
null
null Array of not-found template files or a single file name as a string.
show_detailed_errors bool false If set to [true] then full error details will be displayed on the default error template. When using the default error template if running directly on localhost (both client and server) then full error details will automatically be displayed. These rules would only apply to custom error templates if they are setup the same.
error_page_title string
null
"An error has occurred" Title for 500 Error Responses, available as [$page_title] for the error template.
error_page_message string
null
"An error has occurred while processing your request." Message for 500 Error Responses, available as [$message] for the error template.
not_found_page_title string
null
"Page Not Found" Title for 404 'Not Found' Responses, available as [$page_title] for the template.
not_found_page_message string
null
"The requested page could not be found." Message for 404 'Not Found' Responses, available as [$message] for the template.
method_not_allowed_title string
null
"Error - Method Not Allowed" Title for 405 'Method Not Allowed' Responses, available as [$page_title] for the template.
method_not_allowed_message string
null
"A [{method}] request was submitted however this route only allows for [{allowed_methods}] methods." Message for 405 'Method Not Allowed' Responses, available as [$message] for the template.
strict_url_mode bool false By default, a request for '/about/' with route '/about' will match, however if [strict_url_mode] is set to true then '/about/' and '/about' would be separate URL's.

Methods

setup($timezone)

Setup error handling and optionally set a time-zone for the application

exceptionHandler($e)

Application defined exception handler function

errorHandler($severity, $message, $file, $line)

Application defined error handler function

Returns: bool

shutdown()

Application defined error handler function for fatal errors

escape($text)

Convert special characters to HTML entities. This function is a wrapper for the php function:
    htmlspecialchars($text, ENT_QUOTES, 'UTF-8', true)

Characters escaped are:
    " = &quot;
    & = &amp;
    ' = &#039;
    < = &lt;
    > = &gt;

Returns: string

render($templates, $data = null)

Render a single template file or an array of template files using variables specified in the $data parameter.

Returns: string

route($pattern, \Closure $callback, $method = null)

Add a route for an HTTP Request

Returns: Route

get($pattern, \Closure $callback)

Add a route for an HTTP 'GET' Request

Returns: Route

post($pattern, \Closure $callback)

Add a route for an HTTP 'POST' Request

Returns: Route

routes()

Return the Array of Defined Routes

Returns: array

redirect($url, $status_code = 302)

Redirect the user to another page or site. This must be called prior to headers and content being sent to the user. Defaults to a [302 'Found'] Response.

Example:

    // User makes this request
    $app->get('/page1', function() use ($app) {
        $app->redirect('page2');
    });

    // User will then see this URL and Response
    $app->get('/page2', function() {
        return 'page2';
    });

requestedPath()

Return the Requested Path (Page only, excluding site, base directory, query strings, etc). This will return the same result regardless of the Web Server used and it will be based on where the [index.php] or entry PHP file is located.

Returns: string | null

rootUrl()

Return the Site Root URL; the URL returned is the base URL for all pages.

Returns: string | null

rootDir()

Return the Site Root URL; the URL returned is the base URL for all pages.

Returns: string

routeMatches($pattern, $path)

Check if a the current url path matches a defined route.

Returns: array | bool

run()

This is the main function that processes the request, determines the route, and sends a response. Routes, settings, validation rules, etc need to be defined prior to calling this function.