phalcon/cphalcon · error · Phalcon\Http\Response\Exceptions\ResponseServiceUnavailable

A dependency injection container is required to access the '

Error message

A dependency injection container is required to access the 'response' service

What it means

Http\Response\Cookies::checkGetContainer() throws ResponseServiceUnavailable when the cookies manager has no DI container. The manager needs the container internally (e.g. to resolve the response and crypt services) when cookies are set or sent, so a container-less Cookies object cannot work.

Source

Thrown at phalcon/Http/Response/Cookies.zep:357

    /**
     * Set if cookies in the bag must be automatically encrypted/decrypted
     */
    public function useEncryption(bool useEncryption) -> <CookiesInterface>
    {
        let this->useEncryption = useEncryption;

        return this;
    }

    private function checkGetContainer() -> <DiInterface>
    {
        var container;

        let container = this->container;

        if container === null {
            throw new ResponseServiceUnavailable();
        }

        return container;
    }
}

View on GitHub (pinned to b7419de9cd)

Solutions

  1. Call $cookies->setDI($di) before setting or sending cookies
  2. Use the shared 'cookies' service from FactoryDefault, which receives the container automatically
  3. When registering a replacement, let DI construct it: $di->setShared('cookies', \Phalcon\Http\Response\Cookies::class);

Example fix

// before
$cookies = new \Phalcon\Http\Response\Cookies();
$cookies->set('theme', 'dark')->send(); // throws: no container

// after
$di = new \Phalcon\Di\FactoryDefault();
$cookies = new \Phalcon\Http\Response\Cookies();
$cookies->setDI($di);
$cookies->set('theme', 'dark')->send();
Defensive patterns

Strategy: validation

Validate before calling

if (null === $cookies->getDI() && null === \Phalcon\Di\Di::getDefault()) {
    throw new \RuntimeException('Cookies manager needs a DI container: call setDI()');
}
$cookies->set('name', 'value')->send();

Try / catch

try { $cookies->send(); } catch (\Phalcon\Http\Response\Exceptions\ResponseServiceUnavailable $e) { // wiring bug: attach container and retry
    $cookies->setDI($di);
    $cookies->send();
}

Prevention

When it happens

Trigger: $cookies = new Cookies(); $cookies->set('name', 'value')->send(); without ever calling setDI(); registering a replacement 'cookies' service as a bare instance that never receives the container.

Common situations: Manually constructed Cookies managers in tests or CLI workers; overriding the 'cookies' DI service with new Cookies instead of a container-resolved definition; using Cookies outside the full application lifecycle.

Related errors


AI-assisted analysis of phalcon/cphalcon@b7419de9cd (2026-08-21). Data as JSON: /api/errors/a7e724dbdbfb8b90. Report an issue: GitHub.