Przewodnik

Infrastruktura

Cache, sesje, uploady, filesystem i klient HTTP bez uzależniania aplikacji od wnętrza frameworka.

Cache dla danych, które można odtworzyć

Używaj CacheManager dla danych, które można ponownie obliczyć, na przykład list artykułów lub nawigacji. Cache nie jest źródłem prawdy.

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 [];
        });
    }
}

Sesje i komunikaty flash

W controllerach flash() jest wygodnym sposobem na jednorazowe komunikaty po redirectcie. Poza controllerami jawnie wstrzykuj abstrakcję sesji lub flash.

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

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

Uploady i filesystem

Do uploadów używaj upload() ze skonfigurowanymi profilami, a do pracy z plikami filesystem(). Limity i katalogi docelowe trzymaj w konfiguracji YAML.

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

Klient HTTP

Dla integracji wychodzących wstrzykuj Psr\Http\Client\ClientInterface razem z PSR request factory. Timeouty i SSL konfiguruj w 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);
    }
}