Cors setting not working what's wrong with my code?

I have created middleware and implement the following code but not working. Why ?

public function addHeaders(ServerRequestInterface $request, ResponseInterface $response): ResponseInterface
        if ($request->getHeader('Origin')) {
            $response = $response
                ->withHeader('Access-Control-Allow-Origin', $this->_allowOrigin($request))
                ->withHeader('Access-Control-Allow-Credentials', $this->_allowCredentials())
                ->withHeader('Access-Control-Max-Age', $this->_maxAge());

            if (strtoupper($request->getMethod()) === 'OPTIONS') {
                $response = $response
                    ->withHeader('Access-Control-Expose-Headers', $this->_exposeHeaders())
                    ->withHeader('Access-Control-Allow-Headers', $this->_allowHeaders($request))
                    ->withHeader('Access-Control-Allow-Methods', $this->_allowMethods());


        return $response;

You need to give a bit more context here.
CORS is always related to 2 different domains: the client domain and the server domain.

What HTTP request do you perform form which domain and receive what error?

I am using HTTP Post request and client domain is react app and server domain is cakephp app.