request = $request; $this->response = $response; $this->logger = $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 integer $duration The number of seconds this link should be * considered secure for. Only with HSTS header. * Default value is 1 year. * * @throws \CodeIgniter\HTTP\Exceptions\HTTPException */ protected 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 integer $time */ protected 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 input data. If validation * is not successful, a $errors property will be set on this class. * * @param array|string $rules * @param array $messages An array of custom error messages * * @return boolean */ protected function validate($rules, array $messages = []): bool { $this->validator = Services::validation(); // If you replace the $rules array with the name of the group if (is_string($rules)) { $validation = new \Config\Validation(); // If the rule wasn't found in the \Config\Validation, we // should throw an exception so the developer can find it. if (! isset($validation->$rules)) { throw ValidationException::forRuleNotFound($rules); } // If no error message is defined, use the error message in the Config\Validation file if (! $messages) { $errorName = $rules . '_errors'; $messages = $validation->$errorName ?? []; } $rules = $validation->$rules; } $success = $this->validator ->withRequest($this->request) ->setRules($rules, $messages) ->run(); return $success; } //-------------------------------------------------------------------- }