prg-pattern
/prg-pattern
Wdraża wzorzec Post/Redirect/Get w obsłudze formularzy i punktów POST: sukces przekierowuje, błąd renderuje formularz ponownie, a komunikaty flash pojawiają się po przekierowaniu.
- Autor
- fabiensalles ↗
- Repozytorium
- claude-marketplace ↗
- Licencja
- MIT
- Ostatnia zmiana repozytorium
- 12.10.2026
Ocena i bezpieczeństwo
- Pliki w paczce
- 1
- Skrypty
- 0
- Komentarze HTML
- 0
- Adresy URL w SKILL.md
- 0
Brak sygnałów. Repozytorium zmienione 12.10.2026, bez odwołań do wycofanych modeli i API.
MIT, otwarta: można używać i zmieniać, z zachowaniem informacji o autorze.
Co robi i kiedy
Wdraża wzorzec Post/Redirect/Get w obsłudze formularzy i punktów POST: sukces przekierowuje, błąd renderuje formularz ponownie, a komunikaty flash pojawiają się po przekierowaniu.
Wyzwalacze z opisu
Oryginalny opis (EN)
ACTIVATE when implementing form handling, POST endpoints, or any controller that modifies data. ACTIVATE for 'form submission', 'POST redirect', 'PRG', 'duplicate submission'. Covers: POST success always redirects, POST error re-renders (no redirect), flash messages after redirect. DO NOT use for: FormType design (see symfony:symfony-form), API endpoints returning JSON.
Jak działa
- The Solution
- Implementation Rules
- Complete Example
- Quick Reference
- Anti-Patterns
Podgląd SKILL.md
---
name: prg-pattern
description: "ACTIVATE when implementing form handling, POST endpoints, or any controller that modifies data. ACTIVATE for 'form submission', 'POST redirect', 'PRG', 'duplicate submission'. Covers: POST success always redirects, POST error re-renders (no redirect), flash messages after redirect. DO NOT use for: FormType design (see symfony:symfony-form), API endpoints returning JSON."
---
# Post/Redirect/Get (PRG) Pattern
POST success -> redirect. POST error -> re-render with errors. This prevents duplicate submissions on page refresh.
## The Solution
```
┌─────────────────────────────────────────────────────────────┐
│ PRG FLOW │
├─────────────────────────────────────────────────────────────┤
│ │
│ GET /form ──────────► Display Form │
│ ▲ │ │
│ │ ▼ │
│ │ User fills & submits │
│ │ │ │
│ │ ▼ │
│ ┌────┴────┐ POST /form │
│ │ Refresh │ │ │
│ │ safe! │ ▼ │
│ └────┬────┘ ┌──────────┴──────────┐ │
│ │ │ │ │
│ │ Validation OK? Validation KO │
│ │ │ │ │
│ │ ▼ ▼ │
│ │ 302 REDIRECT Re-render form │
│ │ │ with errors │
│ │ ▼ │
│ └──── GET /success │
│ │
└─────────────────────────────────────────────────────────────┘
```
## Implementation Rules
### 1. POST Success → Always Redirect
```php
if ($request->isMethod('POST')) {
$selection = $request->request->getString('choice');
if ($this->isValid($selection)) {
// ✅ Success: REDIRECT (never return HTML)
return new RedirectResponse(
$this->urlGenerator->generate('next_step')
);
}
// ❌ Error: Re-render form (no redirect)
return $this->render(..., hasError: true);
}
```
### 2. POST Error → Re-render (No Redirect)
When validation fails, **re-render the form** with error messages. This is NOT a violation of PRG because:
- User needs to see their input + errors
- Refreshing will re-submit, but that's intentional (user correcting data)
- Redirecting would lose the submitted data
### 3. Flash Messages for User Feedback
When redirecting after success, use flash messages:
```php
// In controller
$this->session->getFlashBag()->add('success', 'Operation completed');
return new RedirectResponse(...);
// In template
{% for message in app.flashes('success') %}
<div class="alert alert-success">{{ message }}</div>
{% endfor %}
```
## Complete Example
```php
#[Route(path: '/form', name: 'my_form', methods: ['GET', 'POST'])]
public function __invoke(Request $request): Response
{
if ($request->isMethod('POST')) {
$data = $request->request->all();
if (!$this->validate($data)) {
// Error: re-render with data
return $this->render($data, hasError: true);
}
// Success: process and redirect
$this->process($data);
return new RedirectResponse(
$this->urlGenerator->generate('success_page')
);
}
// GET: display empty form
return $this->render();
}
```
## Quick Reference
| Scenario | Response | Why |
|----------|----------|-----|
| GET request | Render form | Display form |
| POST + valid | **RedirectResponse** | PRG: prevent re-submit |
| POST + invalid | Render with errors | Show validation errors |
| POST + exception | Render with error | Show error message |
## Anti-Patterns
```php
// ❌ WRONG: Returning HTML after successful POST
if ($valid) {
$this->save($data);
return new Response('Success!'); // Refresh will re-submit!
}
// ❌ WRONG: Redirecting on error (loses user input)
if (!$valid) {
return new RedirectResponse('/form?error=1');
}
// ✅ CORRECT
if ($valid) {
$this->save($data);
return new RedirectResponse('/success');
}
return $this->renderWithErrors($data);
```
Wersje
W bazie nie ma innych skilli o nazwie prg-pattern.
Migawka bazy z 13.11.2026 · wersja paczki 3d5f307af160 (skrót SHA-256 całej paczki)
Pobrania w ostatnich 30 dniach: 0 · ranking
Zgłoś problem z tym skillem: szkodliwa treść, błędny opis albo prośba autora o usunięcie.
Podobne skille
- 100symfony-form
Pisze i poprawia klasy FormType w Symfony: data_class jako źródło danych, DataTransformery poza kontrolerem i property_path. - 100symfony-test-conventions
Pisze testy Symfony z uruchomionym jądrem: wybiera klasę bazową, testy funkcjonalne, FormType i zastępstwa usług w kontenerze. - 95react-form-validator
Wymusza wzorzec React Hook Form z Zod przy tworzeniu formularzy, walidacji i obsłudze wysyłki, w tym formularzy logowania, rejestracji i wprowadzania danych. - 100oauth-pkce-sessionstorage-lost-on-redirect
Naprawia błędy „Session not found” i brak code_verifier w PKCE po przekierowaniu z zewnętrznego serwera autoryzacji w aplikacji SPA, gdy sessionStorage ginie przy przekierowaniu. - 100redirect-301-generator
Generuje przekierowania 301 na podstawie mapowania adresów źródłowych na docelowe, dopasowane do platformy wdrożeniowej. - 100rest-controller-conventions
Pisze ręczne kontrolery REST w Symfony bez API Platform: atrybuty routingu, kody statusu, mapowanie payloadu i wersjonowanie.