PHP code example of adaiasmagdiel / erlenmeyer

1. Go to this page and download the library: Download adaiasmagdiel/erlenmeyer 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/ */

    

adaiasmagdiel / erlenmeyer example snippets



AdaiasMagdiel\Erlenmeyer\App;
use AdaiasMagdiel\Erlenmeyer\Request;
use AdaiasMagdiel\Erlenmeyer\Response;

$app = new App();

$app->get('/', function (Request $req, Response $res) {
    $res->withHtml("<h1>Welcome to Erlenmeyer!</h1>")->send();
});

$app->run();

$app->addMiddleware(function (Request $req, Response $res, callable $next) {
    $start = microtime(true);
    $next($req, $res); // Calls the next handler in the chain
    $time = microtime(true) - $start;
    echo "<p>Execution time: $time seconds</p>";
});

$app->get('/protected', function (Request $req, Response $res) {
    $res->withHtml("<h1>Protected Route</h1>")->send();
}, [function (Request $req, Response $res, callable $next) {
    if (!isset($_SESSION['user'])) {
        $res->redirect('/login');
    } else {
        $next($req, $res);
    }
}]);

$app->get('/api/data', function (Request $req, Response $res) {
    $res->setCORS([
        'origin' => 'https://my-site.com',
        'methods' => ['GET', 'POST'],
        'headers' => ['Content-Type']
    ]);
    $res->withJson(['data' => 'example'])->send();
});

$app->addMiddleware(function (Request $req, Response $res, callable $next) {
    $res->setCORS([
        'origin' => '*',
        'methods' => 'GET,POST',
        'headers' => 'Content-Type'
    ]);
    $next($req, $res);
});

$app = new App(__DIR__ . '/my-assets', '/static');