Návod

Infrastruktura

Cache, session, uploady, filesystem a HTTP klient bez vazby na interní části frameworku.

Cache pro data, která lze znovu sestavit

Používej CacheManager pro data, která lze znovu spočítat, například seznamy článků nebo navigaci. Cache není zdroj pravdy.

example.php
use Lemonade\Framework\Cache\CacheManager;

final class ArticleFeed
{
    public function __construct(
        private readonly CacheManager $cache,
    ) {}

    public function latest(): array
    {
        return $this->cache->remember('articles.latest', 600, function (): array {
            return [];
        });
    }
}

Session a flash zprávy

Uvnitř controllerů je flash() pohodlná cesta pro jednorázové zprávy přes redirect. Mimo controllery předávej session nebo flash abstrakci explicitně.

example.php
$this->flash()->set('success', 'Article saved.');

return $this->redirect($this->url()->route('articles.index'));

Uploady a filesystem

Pro uploady používej upload() s nakonfigurovanými profily a pro práci se soubory filesystem(). Limity i cílové adresáře drž v YAML konfiguraci.

example.php
$image = $this->upload()->uploadImage('image', 'article_image');
$content = $this->filesystem()->read($this->app()->basePath('README.md'));

HTTP klient

Pro odchozí integrace předej Psr\Http\Client\ClientInterface spolu s PSR request factory. Timeouty a SSL nastavuj v HttpClient.yaml.

example.php
<?php

declare(strict_types=1);

namespace App\Services;

use Psr\Http\Client\ClientInterface;
use Psr\Http\Message\RequestFactoryInterface;

final class StatusApi
{
    public function __construct(
        private readonly ClientInterface $client,
        private readonly RequestFactoryInterface $requests,
    ) {}

    public function check(): void
    {
        $request = $this->requests->createRequest('GET', 'https://status.example.test/health');
        $this->client->sendRequest($request);
    }
}