Welcome Guest, Not a member yet? Register   Sign In
error Middleware(filter) ci4
#2

since ci4 dose accept express.js approach  so 
i created class MainService   other service extend from it 
and define function to stop  execution show custom reponse with native  php  
PHP Code:
<?php

namespace Modules\Shared\Libraries;
use 
CodeIgniter\HTTP\Exceptions\HTTPException;

class 
MainMainService  
{

    protected function responseHttpExceptionstring $messageint $code,?string $body null)
    {

        try {

            throw new HttpException($message$code);

        } catch (\Exception $e) {
            header("HTTP/1.1 {$code} {$message}");
            $json json_encode(['error' => $body]);
            echo $json;
        }

        exit();
    }








PHP Code:
<?php namespace Modules\Common\Services;


use 
CodeIgniter\HTTP\Exceptions\HTTPException;
use 
CodeIgniter\HTTP\ResponseInterface;
use 
Modules\Common\Libraries\CustomFile;
use 
Modules\Shared\Interfaces\UrlQueryParamInterface;
use 
Modules\Shared\Libraries\MainMainService;

class  ProfileService extends MainMainService
{
    private \Myth\Auth\Models\UserModel $model;
    private CustomFile $cfs;
    private string $userId;

    public function __construct()
    {
        $this->userId "";
        $this->model new  \Myth\Auth\Models\UserModel();
        $this->cfs = new CustomFile();
    }

    public function setUserId(string $userId): void
    
{
        $this->userId $userId;
    }

    /**
    * index function
    * @method : GET
    * @param UrlQueryParamInterface $urlQueryParam
    * @return array
    */
    public function index(UrlQueryParamInterface $urlQueryParam)
    {
        $result $this->model->select('
        users.id,
      users.email,
      users.username,
      users.first_name,
      users.last_name,
      users.image,
      users.gender,
      users.birthday,
      users.country,
      users.city,
      users.address,
      users.phone,
      users.status_message,
      users.status,
      users.active,
      users.created_at,
      users.updated_at,
      users.deleted_at
        '
)->find($this->userId);

        $data = ['data' => $result];
        return $data;
    }

    /**
    * update function
    * @method : Post
    * @param $id
    * @param \Myth\Auth\Entities\User $entity
    * @throws \ReflectionException
    */
    public function update($id, \Myth\Auth\Entities\User $entity)
    {

        if (is_null($entity)) $this->responseHttpException(lang('Shared.api.validation'), ResponseInterface::HTTP_NOT_FOUND);


        $isExist $this->model->where('id'$id)->first();

        if (is_null($isExist)) $this->responseHttpException(lang('Shared.api.exist'), ResponseInterface::HTTP_NOT_FOUND);
        if ($isExist->image != 'public/upload/profile/default-avatar.jpg') {
            $this->cfs->removeSingleFile(ROOTPATH $isExist->image);
        }


        if (!$this->model->update($id$entity)) {
            helper('shared');
            $message lang('Shared.api.reject');
            $this->responseHttpException($messageResponseInterface::HTTP_BAD_REQUESTserializeMessages($this->model->errors()));

        }


    }



Enlightenment  Is  Freedom
Reply


Messages In This Thread
error Middleware(filter) ci4 - by paliz - 09-01-2021, 06:36 AM
RE: error Middleware(filter) ci4 - by paliz - 09-02-2021, 12:06 AM



Theme © iAndrew 2016 - Forum software by © MyBB