PHP code example of obsidian-moon / engine

1. Go to this page and download the library: Download obsidian-moon/engine library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

obsidian-moon / engine example snippets


// app/Controllers/LandingController.php
class LandingController extends AbstractController
{
    /**
     * Required: Pass the `views` folder configuration to the abstract parent class.
     * Optional: Pass a set of default values which will be handed off to `ViewHandler`
     */
    public function __construct()
    {
        /**
         * Retrieve default data from database/session or declare statically...
         */
        $optionalDefaultValues = [
            'defaultKey1' => 'defaultValue1',
            'defaultKey2' => 'defaultValue2',
            // ...
        ];
        parent::__construct(viewsRoot: VIEWS_ROOT, viewData: $optionalDefaultValues);
    }
}

use ObsidianMoon\Engine\Handlers\ControllerHandler;
use ObsidianMoon\Framework\Controllers\LandingController;
use ObsidianMoon\Engine\Exceptions\FileNotFoundException;
use Symfony\Component\Routing\Exception\MethodNotAllowedException;
use Symfony\Component\Routing\Exception\ResourceNotFoundException;

/**
 * Needs an array containing the following keys `_controller` as follows
 * For Symfony Routing, Replace array with: $matcher->match($request->getPathInfo())
 *
 * Throws FileNotFoundException, Symfony's ResourceNotFoundException, or Symfony's MethodNotAllowedException on error.
 */
try {
    $controller = new ControllerHandler(controller: ['_controller' => [LandingController::class, 'index']]);
    $response = $controller->render(); // Returns Symfony Responce object
} catch (FileNotFoundException | ResourceNotFoundException) {
    // Unable to find that file or route is undefined.
    $response = 'We could not find that page!';
} catch (MethodNotAllowedException) {
    // Sent because of unsupported method, e.g. `$_POST` instead of `$_GET`.
    $response = 'We are unable to process your request. Please try again!';
}

use ObsidianMoon\Engine\Exceptions\FileNotFoundException;
use ObsidianMoon\Engine\Handlers\ExceptionHandler;

/**
 * Setting `admin` to `false` will return the message we pass to `handle()` method. Otherwise, it will return the
 * message originally sent from initial exception.
 */
$exceptions = new ExceptionHandler(admin: false);

/** Useful in conjunction with the `ControllerHandler`. */
try {
    throw new FileNotFoundException('More detailed message for admins');
} catch (FileNotFoundException $e) {
    $message = $exceptions->handle($e, 'A public error message for non-admins and/or production');
}

use ObsidianMoon\Engine\Exceptions\FileNotFoundException;
use ObsidianMoon\Engine\Handlers\ViewHandler;

/** Optional default data that can be pulled from DB, session, or static variables to be used in the views. */
$optionalDefaultData = [
    'defaultKey1' => 'defaultValue1',
    'defaultKey2' => 'defaultValue2',
    // ...
];

try {
    /** Instantiate with VIEWS_ROOT constant set to `src/views` and prepare to make calls */
    $view = new ViewHandler(viewsRoot: VIEWS_ROOT, viewData: $optionalDefaultData);

    /** Load a view `src/views/landing/index.php`, pass it data, and return output to a variable */
    $landingContent = $view->load(view: 'landing/index', data: ['key1' => 'value1'], return: true)

    /** Take the landing content and insert it as a variable into `src/views/layouts/shell.php` */
    $view->load(view: 'layouts/shell', data: compact('landingContent'));

    /** Render the content that has been stored in the handler output. */
    $view->render();
} catch (FileNotFoundException $e) {
    // Did not find the file, handle the 404 here.
}