PHP Classes

File: app/Base/functions.php

Recommend this page to a friend!
  Classes of Maniruzzaman Akash   Simple, Fast, Lightweight PHP MVC Framework   app/Base/functions.php   Download  
File: app/Base/functions.php
Role: Example script
Content type: text/plain
Description: Example script
Class: Simple, Fast, Lightweight PHP MVC Framework
Route requests to controller classes or closures
Author: By
Last change:
Date: 1 year ago
Size: 2,811 bytes
 

Contents

Class file image Download
<?php

use Pecee\SimpleRouter\SimpleRouter as Router;
use
Pecee\Http\Url;
use
Pecee\Http\Response;
use
Pecee\Http\Request;

/**
 * Make the view file.
 *
 * @param string $path
 * @param array $data
 *
 * @return void
 */
function views(string $path, array $data = []): void
{
   
// Extract the data array to use each value as variable
   
extract($data);

   
// Start output buffering
   
require_once VIEWS . '/' . $path;
}

/**
 * Get environment variable data by key.
 *
 * @param string $key
 * @return string
 */
function env(string $key): string
{
    return isset(
$_ENV[$key]) ? $_ENV[$key] : '';
}

/**
 * Get asset url.
 *
 * @param string $path
 *
 * @return string
 */
function assets(string $path): string
{
    return
ASSET_URL . '/' . $path;
}

/**
 * Get url for a route by using either name/alias, class or method name.
 *
 * The name parameter supports the following values:
 * - Route name
 * - Controller/resource name (with or without method)
 * - Controller class name
 *
 * When searching for controller/resource by name, you can use this syntax "route.name@method".
 * You can also use the same syntax when searching for a specific controller-class "MyController@home".
 * If no arguments is specified, it will return the url for the current loaded route.
 *
 * @param string|null $name
 * @param string|array|null $parameters
 * @param array|null $getParams
 * @return \Pecee\Http\Url
 * @throws \InvalidArgumentException
 */
function url(?string $name = null, $parameters = null, ?array $getParams = null): Url
{
   
$name = BASE_DIR . '/' . $name;
    return
Router::getUrl($name, $parameters, $getParams);
}

/**
 * @return \Pecee\Http\Response
 */
function response(): Response
{
    return
Router::response();
}

/**
 * @return \Pecee\Http\Request
 */
function request(): Request
{
    return
Router::request();
}

/**
 * Get input class
 * @param string|null $index Parameter index name
 * @param string|mixed|null $defaultValue Default return value
 * @param array ...$methods Default methods
 * @return \Pecee\Http\Input\InputHandler|array|string|null
 */
function input($index = null, $defaultValue = null, ...$methods)
{
    if (
$index !== null) {
        return
request()->getInputHandler()->value($index, $defaultValue, ...$methods);
    }

    return
request()->getInputHandler();
}

/**
 * @param string $url
 * @param int|null $code
 */
function redirect(string $url, ?int $code = null): void
{
    if (
$code !== null) {
       
response()->httpCode($code);
    }

   
response()->redirect($url);
}

/**
 * Get current csrf-token
 * @return string|null
 */
function csrf_token(): ?string
{
   
$baseVerifier = Router::router()->getCsrfVerifier();
    if (
$baseVerifier !== null) {
        return
$baseVerifier->getTokenProvider()->getToken();
    }

    return
null;
}