PHP 8.2.32
Preview: Controller.php Size: 4.91 KB
/home/ecopackply/www/system/Controller.php

<?php

declare(strict_types=1);

/**
 * This file is part of CodeIgniter 4 framework.
 *
 * (c) CodeIgniter Foundation <admin@codeigniter.com>
 *
 * For the full copyright and license information, please view
 * the LICENSE file that was distributed with this source code.
 */

namespace CodeIgniter;

use CodeIgniter\HTTP\CLIRequest;
use CodeIgniter\HTTP\Exceptions\HTTPException;
use CodeIgniter\HTTP\Exceptions\RedirectException;
use CodeIgniter\HTTP\IncomingRequest;
use CodeIgniter\HTTP\RequestInterface;
use CodeIgniter\HTTP\ResponseInterface;
use CodeIgniter\Validation\Exceptions\ValidationException;
use CodeIgniter\Validation\ValidationInterface;
use Config\Validation;
use Psr\Log\LoggerInterface;

/**
 * @see \CodeIgniter\ControllerTest
 */
class Controller
{
    /**
     * Helpers that will be automatically loaded on class instantiation.
     *
     * @var list<string>
     */
    protected $helpers = [];

    /**
     * Instance of the main Request object.
     *
     * @var CLIRequest|IncomingRequest
     */
    protected $request;

    /**
     * Instance of the main response object.
     *
     * @var ResponseInterface
     */
    protected $response;

    /**
     * Instance of logger to use.
     *
     * @var LoggerInterface
     */
    protected $logger;

    /**
     * Should enforce HTTPS access for all methods in this controller.
     *
     * @var int Number of seconds to set HSTS header
     */
    protected $forceHTTPS = 0;

    /**
     * Once validation has been run, will hold the Validation instance.
     *
     * @var ValidationInterface|null
     */
    protected $validator;

    /**
     * Constructor.
     *
     * @return void
     *
     * @throws HTTPException|RedirectException
     */
    public function initController(RequestInterface $request, ResponseInterface $response, LoggerInterface $logger)
    {
        $this->request  = $request;
        $this->response = $response;
        $this->logger   = $logger;

        if ($this->forceHTTPS > 0) {
            $this->forceHTTPS($this->forceHTTPS);
        }

        // Autoload helper files.
        helper($this->helpers);
    }

    /**
     * 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.
     *
     * @return void
     *
     * @throws HTTPException|RedirectException
     */
    protected function forceHTTPS(int $duration = 31_536_000)
    {
        force_https($duration, $this->request, $this->response);
    }

    /**
     * How long to cache the current page for.
     *
     * @params int $time time to live in seconds.
     *
     * @return void
     */
    protected function cachePage(int $time)
    {
        service('responsecache')->setTtl($time);
    }

    /**
     * A shortcut to performing validation on Request data.
     *
     * @param array|string $rules
     * @param array        $messages An array of custom error messages
     */
    protected function validate($rules, array $messages = []): bool
    {
        $this->setValidator($rules, $messages);

        return $this->validator->withRequest($this->request)->run();
    }

    /**
     * A shortcut to performing validation on any input data.
     *
     * @param array        $data     The data to validate
     * @param array|string $rules
     * @param array        $messages An array of custom error messages
     * @param string|null  $dbGroup  The database group to use
     */
    protected function validateData(array $data, $rules, array $messages = [], ?string $dbGroup = null): bool
    {
        $this->setValidator($rules, $messages);

        return $this->validator->run($data, null, $dbGroup);
    }

    /**
     * @param array|string $rules
     */
    private function setValidator($rules, array $messages): void
    {
        $this->validator = service('validation');

        // If you replace the $rules array with the name of the group
        if (is_string($rules)) {
            $validation = config(Validation::class);

            // 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};
        }

        $this->validator->setRules($rules, $messages);
    }
}

Directory Contents

Dirs: 41 × Files: 13

Name Size Perms Modified Actions
API DIR
- drwxr-xr-x 2026-01-27 08:07:40
Edit Download
- drwxr-xr-x 2026-01-27 08:07:40
Edit Download
Cache DIR
- drwxr-xr-x 2026-01-27 08:07:40
Edit Download
CLI DIR
- drwxr-xr-x 2026-01-27 08:07:40
Edit Download
Commands DIR
- drwxr-xr-x 2026-01-27 08:07:40
Edit Download
Config DIR
- drwxr-xr-x 2026-01-27 08:07:40
Edit Download
Cookie DIR
- drwxr-xr-x 2026-01-27 08:07:40
Edit Download
Database DIR
- drwxr-xr-x 2026-01-27 08:07:40
Edit Download
- drwxr-xr-x 2026-01-27 08:07:40
Edit Download
- drwxr-xr-x 2026-01-27 08:07:40
Edit Download
Debug DIR
- drwxr-xr-x 2026-01-27 08:07:40
Edit Download
Email DIR
- drwxr-xr-x 2026-01-27 08:07:40
Edit Download
- drwxr-xr-x 2026-01-27 08:07:40
Edit Download
Entity DIR
- drwxr-xr-x 2026-01-27 08:07:40
Edit Download
Events DIR
- drwxr-xr-x 2026-01-27 08:07:40
Edit Download
- drwxr-xr-x 2026-01-27 08:07:40
Edit Download
Files DIR
- drwxr-xr-x 2026-01-27 08:07:40
Edit Download
Filters DIR
- drwxr-xr-x 2026-01-27 08:07:40
Edit Download
Format DIR
- drwxr-xr-x 2026-01-27 08:07:40
Edit Download
Helpers DIR
- drwxr-xr-x 2026-01-27 08:07:40
Edit Download
Honeypot DIR
- drwxr-xr-x 2026-01-27 08:07:40
Edit Download
- drwxr-xr-x 2026-01-27 08:07:40
Edit Download
HTTP DIR
- drwxr-xr-x 2026-01-27 08:07:40
Edit Download
I18n DIR
- drwxr-xr-x 2026-01-27 08:07:40
Edit Download
Images DIR
- drwxr-xr-x 2026-01-27 08:07:40
Edit Download
Language DIR
- drwxr-xr-x 2026-01-27 08:07:40
Edit Download
Log DIR
- drwxr-xr-x 2026-01-27 08:07:40
Edit Download
Modules DIR
- drwxr-xr-x 2026-01-27 08:07:40
Edit Download
Pager DIR
- drwxr-xr-x 2026-01-27 08:07:40
Edit Download
Publisher DIR
- drwxr-xr-x 2026-01-27 08:07:40
Edit Download
RESTful DIR
- drwxr-xr-x 2026-01-27 08:07:40
Edit Download
Router DIR
- drwxr-xr-x 2026-01-27 08:07:40
Edit Download
Security DIR
- drwxr-xr-x 2026-01-27 08:07:40
Edit Download
Session DIR
- drwxr-xr-x 2026-01-27 08:07:40
Edit Download
Test DIR
- drwxr-xr-x 2026-01-27 08:07:40
Edit Download
- drwxr-xr-x 2026-01-27 08:07:40
Edit Download
Throttle DIR
- drwxr-xr-x 2026-01-27 08:07:40
Edit Download
Traits DIR
- drwxr-xr-x 2026-01-27 08:07:40
Edit Download
- drwxr-xr-x 2026-01-27 08:07:40
Edit Download
- drwxr-xr-x 2026-01-27 08:07:40
Edit Download
View DIR
- drwxr-xr-x 2026-01-27 08:07:40
Edit Download
118 B lrw-r--r-- 2026-01-27 08:07:40
Edit Download
57.03 KB lrw-r--r-- 2026-01-27 08:07:40
Edit Download
10.93 KB lrw-r--r-- 2026-01-27 08:07:40
Edit Download
4.77 KB lrw-r--r-- 2026-01-27 08:07:40
Edit Download
32.58 KB lrw-r--r-- 2026-01-27 08:07:40
Edit Download
38.04 KB lrw-r--r-- 2026-01-27 08:07:40
Edit Download
5.25 KB lrw-r--r-- 2026-01-27 08:07:40
Edit Download
4.91 KB lrw-r--r-- 2026-01-27 08:07:40
Edit Download
131 B lrw-r--r-- 2026-01-27 08:07:40
Edit Download
30.16 KB lrw-r--r-- 2026-01-27 08:07:40
Edit Download
1.28 KB lrw-r--r-- 2026-01-27 08:07:40
Edit Download
1.28 KB lrw-r--r-- 2026-01-27 08:07:40
Edit Download
2.92 KB lrw-r--r-- 2026-01-27 08:07:40
Edit Download

If ZipArchive is unavailable, a .tar will be created (no compression).