I have an ‘Site’ Controller for managing SEO:
<?php
return function ($page, $kirby, $site) {
$seo = $kirby->controller('seo' , compact('page', 'site', 'kirby'));
// Override Meta Title
$metatitle = $page->seotitle();
$data = compact('metatitle');
return a::merge($seo, $data);
};
On my Contact page I have a controller to manage the contact form.
<?php
return function($kirby, $pages, $page, $site) {
$alert = null;
if($kirby->request()->is('POST') && get('submit')) {
// check the honeypot
if(empty(get('website')) === false) {
go($page->url());
exit;
}
$data = [
'name' => get('name'),
'email' => get('email'),
'tel' => get('tel'),
'text' => get('text')
];
$rules = [
'name' => ['required', 'minLength' => 3],
'email' => ['required', 'email'],
'tel' => ['required', 'minLength' => 11],
'text' => ['required', 'minLength' => 3, 'maxLength' => 3000],
];
$messages = [
'name' => 'Please enter a valid name',
'email' => 'Please enter a valid email address',
'tel' => 'Please enter a valid telephone number',
'text' => 'Please enter a text between 3 and 3000 characters'
];
// some of the data is invalid
if($invalid = invalid($data, $rules, $messages)) {
$alert = $invalid;
// the data is fine, let's send the email
} else {
try {
$kirby->email([
'template' => 'email',
'from' => 'website@moore-dm.co.uk',
'replyTo' => $data['email'],
'to' => 'david@moore-dm.co.uk',
'subject' => esc($data['name']) . ' sent you a message from your website contact form',
'data' => [
'text' => esc($data['text']),
'sender' => esc($data['name']),
'tel' => esc($data['tel']),
'email' => esc($data['email'])
]
]);
} catch (Exception $error) {
if(option('debug')):
$alert['error'] = '<span class="error">The form could not be sent: <strong>' . $error->getMessage() . '</strong></span>';
else:
$alert['error'] = '<span class="error">The form could not be sent!</span>';
endif;
}
// no exception occurred, let's send a success message
if (empty($alert) === true) {
$success = '<span>Your message has been sent, thank you. We will get back to you soon!</span>';
$data = [];
}
}
}
return [
'alert' => $alert,
'data' => $data ?? false,
'success' => $success ?? false
];
};
How can I add / combine the Site SEO controller to my contact page Controller?