Post Link
A link submitted as a form, with optional HTTP method spoofing, CSRF protection, and a confirmation prompt.
<twig:PostLink href="/link/newsletter/subscribe" class="">
Subscribe
</twig:PostLink>
Installation
Available since UX Toolkit 3.4.
php bin/console ux:install post-link --kit common
Install the following Composer dependencies:
composer require symfony/security-csrf
Copy the following file(s) into your app:
{% props
## string The URL the form submits to.
href,
## string|null The CSRF token ID used to generate the hidden token field; when null, no token is added.
csrfTokenId = null,
## string The HTTP method used to submit the form; when not `POST`, a hidden `_method` field is added for method spoofing.
method = 'POST',
## string|null A confirmation message shown before submitting; when null, no confirmation is required.
confirm = null
%}
<form{{ attributes.nested('form').defaults({
action: href,
method: 'post',
class: 'inline',
onsubmit: confirm ? 'return confirm(' ~ confirm|json_encode ~ ')' : false,
}) }}>
{% if method|upper != 'POST' %}
<input type="hidden" name="_method" value="{{ method|upper }}">
{% endif %}
{% if csrfTokenId %}
<input{{ attributes.nested('csrf').defaults({
type: 'hidden',
name: '_csrf_token',
'data-controller': 'csrf-protection',
value: csrf_token(csrfTokenId),
}) }}>
{% endif %}
<button{{ attributes.defaults({type: 'submit'}) }}>
{## The button label. -#}
{% block content %}{% endblock %}
</button>
</form>
Usage
<twig:PostLink href="/posts/42/publish">
Publish
</twig:PostLink>
Examples
Custom Method
Set the method prop to submit the form with a spoofed HTTP method. A hidden _method field is added so Symfony can route the request to the matching controller.
Method spoofing only works when HTTP method override is enabled in your Symfony app. Set framework.http_method_override: true in config/packages/framework.yaml.
<twig:PostLink href="/link/posts/42" method="DELETE" class="">
Delete post
</twig:PostLink>
With Confirmation
Pass a confirm message to prompt the user with a native confirmation dialog before the form is submitted.
<twig:PostLink href="/link/posts/42/delete" confirm="Are you sure you want to delete this post?" class="">
Delete post
</twig:PostLink>
With CSRF Protection
Set csrfTokenId to add a hidden CSRF token field, protecting the form against cross-site request forgery.
<twig:PostLink href="/link/posts/42/delete" csrfTokenId="delete-post" class="">
Delete post
</twig:PostLink>
API Reference
<twig:PostLink>
| Prop | Type | Default |
|---|---|---|
href The URL the form submits to.
|
string |
- |
csrfTokenId The CSRF token ID used to generate the hidden token field; when null, no token is added.
|
string|null |
null |
method The HTTP method used to submit the form; when not
POST, a hidden _method field is added for method spoofing. |
string |
'POST' |
confirm A confirmation message shown before submitting; when null, no confirmation is required.
|
string|null |
null |
| Block | Description |
|---|---|
content |
The button label. |