Last active
July 3, 2020 08:17
-
-
Save mficzel/ae75da88cc56f0bb378be113acc557e6 to your computer and use it in GitHub Desktop.
Contact-form example with Neos.Fusion.Form and a custom controller
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace Vendor\Site\Controller; | |
use Neos\Flow\Annotations as Flow; | |
use Neos\Flow\Mvc\Controller\ActionController; | |
use Neos\SwiftMailer\Message as SwiftMailerMessage; | |
class ContactController extends ActionController | |
{ | |
/** | |
* @var string | |
* @Flow\InjectConfiguration(path="contact.recipient") | |
*/ | |
protected $recipient; | |
/** | |
* @var string | |
* @Flow\InjectConfiguration(path="contact.sender") | |
*/ | |
protected $sender; | |
/** | |
* @var string | |
* @Flow\InjectConfiguration(path="contact.subject") | |
*/ | |
protected $subject; | |
/** | |
* @var string | |
* @Flow\InjectConfiguration(path="contact.thankYouPath") | |
*/ | |
protected $thankYouPath; | |
/** | |
* @param string $name | |
* @param string $email | |
* @param string $phone | |
* @param string $message | |
* | |
* @Flow\Validate(argumentName="name", type="NotEmpty") | |
* @Flow\Validate(argumentName="email", type="EmailAddress") | |
* @Flow\Validate(argumentName="email", type="NotEmpty") | |
* @Flow\Validate(argumentName="phone", type="NotEmpty") | |
* @Flow\Validate(argumentName="message", type="NotEmpty") | |
*/ | |
public function contactAction(string $name, string $email, string $phone, string $message) | |
{ | |
$mail = new SwiftMailerMessage(); | |
->setFrom($email, $name) | |
->setSubject($this->subject); | |
$mail->setTo($this->recipient); | |
// build body | |
$content = ""; | |
$content .= "Name: " . strip_tags($name) .chr(10); | |
$content .= "Email: " . strip_tags($email) .chr(10); | |
$content .= "Phone: " . strip_tags($phone).chr(10); | |
$content .= "Message: " .chr(10) . strip_tags($message); | |
$mail->setBody($content); | |
// send | |
$mail->send(); | |
// redirect | |
$uri = $this->controllerContext->getRequest()->getHttpRequest()->getUri(); | |
$uri = $uri->withPath($this->thankYouPath)->withQuery(''); | |
$this->redirectToUri($uri); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
renderer = afx` | |
<Neos.Fusion.Form:Form | |
form.target.package="Vendor.Site" | |
form.target.controller="Contact" | |
form.target.action="contact" | |
> | |
<Neos.Fusion.Form:Textfield | |
field.name="name" | |
attributes.placeholder="Name" | |
attributes.required="" /> | |
<Neos.Fusion.Form:Input | |
field.name="email" | |
attributes.type="email" | |
attributes.placeholder="Email" | |
attributes.required="" /> | |
<Neos.Fusion.Form:Textfield | |
field.name="phone" | |
attributes.type="text" | |
attributes.placeholder="Telefon" | |
attributes.required="" /> | |
<Neos.Fusion.Form:Textarea | |
field.name="message" | |
attributes.rows="6" | |
attributes.placeholder="Nachricht" | |
attributes.required="" | |
></Neos.Fusion.Form:Textarea> | |
<Neos.Fusion.Form:Button attributes.type="submit">Submit</Neos.Fusion.Form:Button> | |
</Neos.Fusion.Form:Form> | |
` | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
privilegeTargets: | |
'Neos\Flow\Security\Authorization\Privilege\Method\MethodPrivilege': | |
'Vendor.Site:Contact': | |
matcher: 'method(Vendor\Site\Controller\ContactController->contactAction())' | |
roles: | |
'Neos.Flow:Everybody': | |
privileges: | |
- | |
privilegeTarget: 'Vendor.Site:Contact' | |
permission: GRANT |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
- | |
name: 'Contact' | |
uriPattern: 'contact/email' | |
defaults: | |
'@package': 'Vendor.Site' | |
'@controller': 'Contact' | |
'@action': 'contact' | |
'@format': 'html' | |
appendExceedingArguments: true | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Neos: | |
Flow: | |
mvc: | |
routes: | |
Vendor.Site: | |
position: 'before Neos.Neos' | |
Vendor: | |
Site: | |
contact: | |
recipient: "[email protected]" | |
subject: "Contact form was sent" | |
from: "[email protected]" | |
thankYouPath: "/thankyou.html" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment