Base extension
On this page
Purpose
Start with a BaseExtension if this extension is
In most cases this is the case when implementing event hooks like reacting on logins.
Extension class
To create a new base extension extend the Iacbox\Loginpage\Extension\Extension
class.
Class reference
/**
* Returns the priority of extension which defaults to LoginpageCallback::PRIO_DEFAULT = 50.
* Override this method to return a higher or lower priority for this extension. This influences the order of executed handlers.
* @return int Priority of Extension (0 = lowest; 100 = highest) - default is 50
*/
public function getPriority():int {}
/**
* Returns name which is used for extension in login page editor
* Override this method to set display name of the extension
* @return string Name displayed in editor
*/
public function getDisplayName():string {}
/**
* Runs immeadiatly after extension is loaded
* Override this method to run code immediately after loading extension
* @return void
*/
public function onLoaded() {}
/**
* Returns instance of RenderHelper Class
* @return RenderHelper Instance of RenderHelper
*/
public function getRenderHelper():RenderHelper {}
/**
* Check if code is called in login page editor
* @return bool true if called in login page editor
*/
public function inPreviewMode():bool {}
/**
* Check if code is called in login page
* @return bool true if called in login page
*/
public function inProductionMode():bool {}
/**
* Logs error for debugging
* @param string $message Message displayed
* @param Throwable $error Php error object for message and stack trace
* @return void
*/
public function logError(string $message, Throwable $error = null) {}
/**
* Logs warning for debugging
* @param string $message Message displayed
* @return void
*/
public function logWarning(string $message) {}
/**
* Logs info for debugging
* @param string $message Message displayed
* @return void
*/
public function logInfo(string $message) {}
/**
* Returns config for extension set in login page editor
* @param Request $request Request from login page
* @param bool $forceReload force reloading of config
* @return array Config hash from extension config or empty hash if no config found
*/
protected function getPageConfig():?array {}
/**
* Do not overwrite in extensions!
* Required for correct working of extensions in login page
*/
public function __construct(Router $router, Translations $translations) {}
Example
A small extension which reacts on a successful login
<?php
declare(strict_types=1);
namespace Iacbox\Loginpage\Extension\Example;
use Iacbox\Loginpage\Extension\Extension;
use Iacbox\Loginpage\Extension\FormPostLeaveListener;
use Iacbox\Loginpage\Extension\FormPostEntryListener;
use Iacbox\Loginpage\Request;
use Iacbox\Loginpage\Session;
use Iacbox\Loginpage\Response;
use Iacbox\Loginpage\Html\HtmlBuilder;
use \DOMElement;
class ExampleExtension extends Extension implements FormPostLeaveListener, FormPostEntryListener {
// variable to hold online state before login page logic
protected $wasClientAlreadyOnline = false;
// set name of extension in login page editor
public function getDisplayName():string {
return 'Example Extension';
}
// remember if the client is already online before the froms are processed
public function onFormPostEntry(Request &$request) {
$this->wasClientAlreadyOnline = ($request->getSession()->getState() == Session::ONLINE);
}
public function onFormPostLeave(Request &$request) {
// if state changes from not online to online
if (! $this->wasClientAlreadyOnline && $request->getSession()->getState() == Session::ONLINE) {
$this->logInfo('Client logged in successfully');
// .. do something here
}
}
}