PHP code example of lemmon / garner
1. Go to this page and download the library: Download lemmon/garner 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/ */
lemmon / garner example snippets
declare(strict_types=1);
define('GARNER_PROJECT_ROOT', dirname(__DIR__));
// routes/api/+controller.php
use Garner\Render\RenderedResponse;
return static fn($page, $site, $app) => RenderedResponse::json(['ok' => true]);
return static fn($page, $site, $app) => RenderedResponse::json(['ok' => true])
->withHeader('X-Robots-Tag', 'noindex')
->withCookie('seen', '1');
// routes/subscribe/+action.php
use Garner\Render\ActionResult;
return static function ($request, $page, $site, $app): ActionResult {
$email = trim((string) ($request->form()['email'] ?? ''));
if (filter_var($email, FILTER_VALIDATE_EMAIL) === false) {
return ActionResult::failure([
'error' => 'Enter a valid email address.',
'email' => $email,
]);
}
// ... persist the subscription ...
return ActionResult::redirect('/subscribe/thanks');
};
return ActionResult::failure(['error' => '…'], fragment: 'subscribe_form');
return static function (Environment $twig, Application $app): void {
$twig->addFunction(new TwigFunction('og_image', /* ... */));
};
// routes/subscribe/+action.php
$app->session()->flash('notice', 'Subscribed!');
return ActionResult::redirect('/subscribe/thanks');
// routes/subscribe/thanks/+controller.php
return static fn(Request $request, Page $page, Site $site, Application $app): array => [
'notice' => $app->session()->consumeFlash('notice'),
];
$weather = $app->cache()->remember(
'weather:bardejov',
callback: fn() => fetchWeather(),
ttl: 3600,
);
// routes/subscribe/+action.php
$hash = hash('sha256', strtolower(trim($email)));
if (!$app->store()->add("email:$hash", ['email' => $email, 'created' => gmdate('c')])) {
return ActionResult::failure(['error' => 'Already subscribed.']);
}
sh
php -S localhost:8000 -t public vendor/lemmon/garner/boot/server.php
twig
{% set photo = page.file('team.jpg') %}
{% if photo %}<img src="{{ photo.url }}" alt="{{ photo.get('alt') }}">{% endif %}
{% for image in page.files.images %}
<img src="{{ image.url }}">
{% endfor %}
sh
php bin/garner reindex
sh
php bin/garner cache:clear && php bin/garner reindex