request = $request; $this->response = $response; $this->logger = is_null($logger) ? Services::logger(true) : $logger; $this->logger->info('Controller "'.get_class($this).'" loaded.'); if ($this->forceHTTPS > 0) { $this->forceHTTPS($this->forceHTTPS); } $this->loadHelpers(); } //-------------------------------------------------------------------- /** * A convenience method to use when you need to ensure that a single * method is reached only via HTTPS. If it isn't, then a redirect * will happen back to this method and HSTS header will be sent * to have modern browsers transform requests automatically. * * @param int $duration The number of seconds this link should be * considered secure for. Only with HSTS header. * Default value is 1 year. */ public function forceHTTPS(int $duration = 31536000) { force_https($duration, $this->request, $this->response); } //-------------------------------------------------------------------- /** * Provides a simple way to tie into the main CodeIgniter class * and tell it how long to cache the current page for. * * @param int $time */ public function cachePage(int $time) { CodeIgniter::cache($time); } //-------------------------------------------------------------------- /** * Handles "auto-loading" helper files. */ protected function loadHelpers() { if (empty($this->helpers)) return; foreach ($this->helpers as $helper) { helper($helper); } } //-------------------------------------------------------------------- /** * A shortcut to performing validation on $_POST input. If validation * is not successful, a $errors property will be set on this class. * * @param \CodeIgniter\HTTP\RequestInterface $request * @param $rules * @param array|null $messages * * @return bool */ public function validate(RequestInterface $request, $rules, array $messages = null): bool { $this->validator = Services::validation(); $success = $this->validator->withRequest($request) ->setRules($rules, $messages) ->run(); return $success; } //-------------------------------------------------------------------- }