プラグインをビルドすることができます。ここでは簡単なサンプルコードを示します。この例ではyoursite.com/my-form-route
のサンプルページにフォームを投稿します。
<?php
namespace Grav\Plugin;
use \Grav\Common\Plugin;
class MyAPIPlugin extends Plugin
{
public static function getSubscribedEvents()
{
return [
'onPluginsInitialized' => ['onPluginsInitialized', 0]
];
}
public function onPluginsInitialized()
{
if ($this->isAdmin())
return;
$this->enable([
'onPageInitialized' => ['onPageInitialized', 0],
]);
}
public function onPageInitialized()
{
// This route should be set in the plugin's setting instead of hard-code here.
$myFormRoute = 'my-from-route';
$page = $this->grav['page'];
$currentPageRoute = $page->route();
// This is not the page containing my form. Skip and render the page as normal.
if ($myFormRoute != $currentPageRoute)
return;
// This is page containing my form, check if there is submitted data in $_POST and send it to external API.
if (!isset($_POST['my_form']))
return;
// Send $_POST['my_form'] to external API here.
}
}