Request handler

Purpose

A request handler intercepts some requests, but not all. Depending on specific request parameters that can be defined. Every request passes many built-in handlers - for example a NotFoundHandler that shows a 404 Not Found page.

RequestHandlerExtension class

To create a new custom request handler extend the Iacbox\Loginpage\Extension\RequestHandlerExtension 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 in login page editor
 * Override this method to set display name of the extension in the login page editor
 * @return string Name displayed in editor
 */ 
public function getDisplayName():string {}

/**
 * Runs immeadiatly after ReequestHandler is loaded
 * Override this method to run code immeadiatly afeter extension is loaded
 * @return void
 */
public function onLoaded() {}

/**
 * The method a request is passed to before the login page logic and rendering starts
 * Override this method to process the request before login page logic starts
 * @param Request $request The Request object encapsulation all needed fields and abstracting the super globals away
 * @return Response Response send to login page
 */
public function processRequest(Request &$request):?Response {}

/**
 * Returns an instance of RenderHelper
 * @return RenderHelper Instance of RenderHelper
 */
public function getRenderHelper():RenderHelper {}

/**
 * Check if code is called in the login page editor preview
 * @return bool true if called in login page editor
 */
public function inPreviewMode():bool {}

/**
 * Check if code is called while the production login page is rendered
 * @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 exception 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 {}

/**
 * Creates redirect reponse to login page with get params added
 * @param string $getParam Get params added to url (default = null)
 * @return Response Response send to login page
 */
public function redirToLoginPage(?string $getParam = null):Response {}

// Do not overwrite in extensions!
// Required for correct working extensions

public function setRouter(Router $router) {}
public function __construct(Translations $translations) {}

Response

A handler either

  1. handles the request and returns an Response instance
  2. is not interested in this request and returns null so the next handler in the chain proceeds

Example

<?php

namespace Iacbox\Loginpage\Extension\MyCustomHandler;

use Iacbox\Loginpage\Extension\RequestHandlerExtension;
use Iacbox\Loginpage\Request;
use Iacbox\Loginpage\Session;
use Iacbox\Loginpage\Response;

class MyCustomHandlerExtension extends RequestHandlerExtension {

	public function processRequest(Request $request):?Response {
		// first set a condition that decides if this request should be handled or not
		if (!$request->isPost() || !$request->getPostParam('myVar')) {
			// condition not met - let request pass
			return null;
		}

		// condition met - handle request
		$response = [
			'status' => 'ok',
			'data' => ''
		];
		// return an JSON encoded 
		return Response::createJSONResponse($response);
	}

}