3
declare(strict_types=1);
6
* This file is part of CodeIgniter 4 framework.
8
* (c) CodeIgniter Foundation <admin@codeigniter.com>
10
* For the full copyright and license information, please view
11
* the LICENSE file that was distributed with this source code.
16
use CodeIgniter\HTTP\CLIRequest;
17
use CodeIgniter\HTTP\Exceptions\HTTPException;
18
use CodeIgniter\HTTP\IncomingRequest;
19
use CodeIgniter\HTTP\RequestInterface;
20
use CodeIgniter\HTTP\ResponseInterface;
21
use CodeIgniter\Validation\Exceptions\ValidationException;
22
use CodeIgniter\Validation\ValidationInterface;
24
use Psr\Log\LoggerInterface;
29
* @see \CodeIgniter\ControllerTest
34
* Helpers that will be automatically loaded on class instantiation.
38
protected $helpers = [];
41
* Instance of the main Request object.
43
* @var CLIRequest|IncomingRequest
48
* Instance of the main response object.
50
* @var ResponseInterface
55
* Instance of logger to use.
57
* @var LoggerInterface
62
* Should enforce HTTPS access for all methods in this controller.
64
* @var int Number of seconds to set HSTS header
66
protected $forceHTTPS = 0;
69
* Once validation has been run, will hold the Validation instance.
71
* @var ValidationInterface|null
80
* @throws HTTPException
82
public function initController(RequestInterface $request, ResponseInterface $response, LoggerInterface $logger)
84
$this->request = $request;
85
$this->response = $response;
86
$this->logger = $logger;
88
if ($this->forceHTTPS > 0) {
89
$this->forceHTTPS($this->forceHTTPS);
92
// Autoload helper files.
93
helper($this->helpers);
97
* A convenience method to use when you need to ensure that a single
98
* method is reached only via HTTPS. If it isn't, then a redirect
99
* will happen back to this method and HSTS header will be sent
100
* to have modern browsers transform requests automatically.
102
* @param int $duration The number of seconds this link should be
103
* considered secure for. Only with HSTS header.
104
* Default value is 1 year.
108
* @throws HTTPException
110
protected function forceHTTPS(int $duration = 31_536_000)
112
force_https($duration, $this->request, $this->response);
116
* How long to cache the current page for.
118
* @params int $time time to live in seconds.
122
protected function cachePage(int $time)
124
service('responsecache')->setTtl($time);
128
* A shortcut to performing validation on Request data.
130
* @param array|string $rules
131
* @param array $messages An array of custom error messages
133
protected function validate($rules, array $messages = []): bool
135
$this->setValidator($rules, $messages);
137
return $this->validator->withRequest($this->request)->run();
141
* A shortcut to performing validation on any input data.
143
* @param array $data The data to validate
144
* @param array|string $rules
145
* @param array $messages An array of custom error messages
146
* @param string|null $dbGroup The database group to use
148
protected function validateData(array $data, $rules, array $messages = [], ?string $dbGroup = null): bool
150
$this->setValidator($rules, $messages);
152
return $this->validator->run($data, null, $dbGroup);
156
* @param array|string $rules
158
private function setValidator($rules, array $messages): void
160
$this->validator = service('validation');
162
// If you replace the $rules array with the name of the group
163
if (is_string($rules)) {
164
$validation = config(Validation::class);
166
// If the rule wasn't found in the \Config\Validation, we
167
// should throw an exception so the developer can find it.
168
if (! isset($validation->{$rules})) {
169
throw ValidationException::forRuleNotFound($rules);
172
// If no error message is defined, use the error message in the Config\Validation file
173
if ($messages === []) {
174
$errorName = $rules . '_errors';
175
$messages = $validation->{$errorName} ?? [];
178
$rules = $validation->{$rules};
181
$this->validator->setRules($rules, $messages);