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.

Código Fonte

GitHub

Código de Exemplo

<?php
// Apenas dois arquivos são necessários para rodar o FastSitePHP AppMin e eles
// podem estar no mesmo diretório que o [index.php] ou da página principal.
require 'AppMin.php';
require 'Route.php';

// Crie o Objeto AppMin e opcionalmente configure controle de Erro e
// Fuso Horário.
$app = new FastSitePHP\AppMin();
$app->setup('UTC');

// Define a rota 'Hello World' padrão
$app->get('/', function() {
    return 'Olá Mundo!';
});

// Retorne uma resposta JSON Return a JSON Response retornando um
// Objeto ou um Array
$app->get('/json', function() {
    return ['Olá' => 'Mundo'];
});

// Envie uma Resposta em Texto Puro e um Cabeçalho Personalizado. AppMin tem um
tamanho mínimo, então, parâmetros opcionais de URL como [:name?] e curingas de
// URL [*] não são suportados.
$app->get('/hello/:name', function($name) use ($app) {
    $app->headers = [
        'Content-Type' => 'text/plain',
        'X-Custom-Header' => $name,
    ];
    return 'Olá ' . $name;
});

// Rode a App
$app->run();

Propriedades

Nome Tipo de Dados Padrão Descrição
status_code int
null
null HTTP Response Status Code
headers array [] HTTP Response Headers
cors_headers array 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.
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.

Métodos

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

Retorna: 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;

Retorna: string

render($templates, $data = null)

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

Retorna: string

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

Add a route for an HTTP Request

Retorna: Route

get($pattern, \Closure $callback)

Add a route for an HTTP 'GET' Request

Retorna: Route

post($pattern, \Closure $callback)

Add a route for an HTTP 'POST' Request

Retorna: Route

routes()

Return the Array of Defined Routes

Retorna: 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.

Retorna: string | null

rootUrl()

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

Retorna: string | null

rootDir()

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

Retorna: string

routeMatches($pattern, $path)

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

Retorna: 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.