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
use AdaiasMagdiel\Erlenmeyer\App;
use AdaiasMagdiel\Erlenmeyer\Request;
use AdaiasMagdiel\Erlenmeyer\Response;
$app = new App();
$app->get('/', function (Request $req, Response $res, $params) {
$res->withHtml('Hello, World!')->send();
});
$app->run();
$app->get('/path', $handler);
$app->post('/path', $handler);
$app->put('/path', $handler);
$app->delete('/path', $handler);
$app->patch('/path', $handler);
$app->options('/path', $handler);
$app->head('/path', $handler);
$app->any('/path', $handler); // Matches any HTTP method
$app->match(['GET', 'POST'], '/path', $handler); // Matches specified methods
$app->get('/user/[id]', function (Request $req, Response $res, $params) {
$id = $params->id;
$res->withHtml("User ID: $id")->send();
});
$app->redirect('/old', '/new', false); // Temporary redirect (302)
$app->addMiddleware(function (Request $req, Response $res, callable $next, $params) {
// Middleware logic
$next($req, $res, $params);
});
$app->get('/admin', $handler, [$middleware1, $middleware2]);
$app->set404Handler(function (Request $req, Response $res, $params) {
$res->setStatusCode(404)->withHtml('Custom 404')->send();
});
$app->setExceptionHandler(\Exception::class, function (Request $req, Response $res, \Exception $e) {
$res->setStatusCode(500)->withHtml('Error: ' . $e->getMessage())->send();
});
use AdaiasMagdiel\Erlenmeyer\Session;
Session::set('key', 'value');
$value = Session::get('key', 'default');
Session::flash('message', 'Flash message');
$flash = Session::getFlash('message');
$method = $req->getMethod();
$uri = $req->getUri();
$queryParams = $req->getQueryParams();
$formData = $req->getFormData();
$jsonData = $req->getJson();
$files = $req->getFiles();
$isAjax = $req->isAjax();
$isSecure = $req->isSecure();
$res->withHtml('HTML content');
$res->withJson(['key' => 'value']);
$res->withText('Plain text');
$res->withFile('/path/to/file');
$res->redirect('/path');
$res->setStatusCode(404);
$res->setHeader('Key', 'Value');
$res->setCORS(['origin' => '*', 'methods' => 'GET,POST']);
$res->send();
$app->get('/api/users/[id]', function (Request $req, Response $res, $params) {
$id = $params->id;
$res->withJson(['id' => $id, 'name' => 'User ' . $id])->send();
});
$app->post('/submit', function (Request $req, Response $res, $params) {
$name = $req->getFormDataParam('name', 'Guest');
Session::flash('message', 'Form submitted successfully!');
$res->redirect('/thank-you')->send();
});