*/ protected $helpers = ['security']; /** @var ApiClient */ protected ApiClient $api; /** * Be sure to declare properties for any property fetch you initialized. * The creation of dynamic property is deprecated in PHP 8.2. */ // protected $session; /** * @return void */ public function initController(RequestInterface $request, ResponseInterface $response, LoggerInterface $logger) { // Do Not Edit This Line parent::initController($request, $response, $logger); // Preload any models, libraries, etc, here. // E.g.: $this->session = \Config\Services::session(); session(); // Shared API client available to all controllers $this->api = service('apiClient'); } /** * Get the user role from the session or database * * @return string|null */ protected function getUserRole() { // Assuming the user role is stored in the session return session()->get('role'); } protected function validated(array $rules, ?array $data = null): array { $validation = service('validation'); $payload = $data ?? ($this->request->getJSON(true) ?: $this->request->getPost()); $payload = sanitize_request_value($payload); $validation->setRules($rules); if (! $validation->run($payload)) { throw new \InvalidArgumentException(json_encode($validation->getErrors(), JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES) ?: 'Validation failed'); } return $payload; } } ?>