Skip to content

Getting Started

Jericho Aquino edited this page Jun 14, 2023 · 1 revision

Getting Started

Installation

composer require eru123/holyphp

Autoloading Composer Dependencies

On your index file, assign the value from composer autoload to a variable.

$autoload = require_once __DIR__ . '/vendor/autoload.php';

Use the Composer helper to pass the autoload variable, this will be used to load the configurations, routes, and other files.

eru123\helper\Composer::set_autoload($autoload);

Or if you are lazy like the creator himself to type all that namespacing, you can use the set_autoload function directly.

set_autoload($autoload);

Or if you are still surprisingly lazy, you can just (go fuck and die, just kidding we're all holy ones here) require the autoloader and it will do it itself when it's needed.

require_once __DIR__ . '/vendor/autoload.php';

Loading Environment Variables

From file

eru123\config\DotEnv::load(__DIR__.'/.env');

From Directory

This will look for .env file in the given directory

eru123\config\DotEnv::load(__DIR__);

Enable Strict Mode

Passing a true (boolean) value to the second argument of load function enables the strict mode

eru123\config\DotEnv::load(__DIR__, true);

Enabling strict mode will do the following:

  • check if file or directory exists
  • check if variable name is valid
  • check if variable in the value is defined
  • throws an exception when parsing failed

Runtime Usage

Set env value

$key = 'app.mode';
$value = 'development'
env_set($key, $value);
env_set('app.modes', [
    'development' => [
        'name' => 'Dev'
    ],
    'production' => [
        'name' => 'Dev'
    ],
]);

Get env value

$default = NULL; // default value in case the key is not defined
$mode = env('app.mode', $default); // development

// Using variables
$mode = env('app.modes.${app.mode}.name', $default); // Dev