<p>So your code should actually be:</p>
<pre><code>        {% set cookie = create({
            class: 'yii\\web\\Cookie',
            name: 'letzterBesuch',
            value: now | atom,
        }) %}
</code></pre>
<p>Notice the capitalized <strong>C</strong> in the <code>yii\\web\\Cookie</code> class namespace. <a href="https://stackoverflow.com/questions/33273941/php-case-sensitivity">Class names in PHP are (mostly) case-insensitive</a>, auto-loaders such as used with Composer <a href="https://dotjoeblog.wordpress.com/2019/07/03/php-namespaces-are-case-insensitive-but/" rel="nofollow noreferrer">are case sensitive</a>.</p>
<p>It's a best practice to use the correct casing regardless, IMO.</p>
<p>More: <a href="https://nystudio107.com/blog/cutting-the-cord-removing-plugins#plugin-2-cookies" rel="nofollow noreferrer">https://nystudio107.com/blog/cutting-the-cord-removing-plugins#plugin-2-cookies</a></p>

CC BY-SA 4.0
2022-08-15T06:50:32.080Z
**Thank you so much!**
Small cause with great effect …
Is working!
Andreas Ploch
CC BY-SA 4.0
2022-08-14T18:38:32.510Z
2022-08-14T18:38:32.510Z
andrew.welch
<p><strong>Hello,</strong></p>
<p>within a craft template I query the existence of a cookie to visually highlight new entries in the CMS with CSS. This worked for a long time without any problems.
A visitor told me that on Safari and iOS 15 an error message appears after deleting website data and history in Safari.
I could understand the effect on myself.</p>
<pre><code>Not instantiable – yii\di\NotInstantiableException
Failed to instantiate component or class &quot;yii\web\cookie&quot;.
Caused by: ReflectionException`


1. in /home/www/bernerfotos.de/vendor/yiisoft/yii2/di/Container.phpat line 509
500501502503504505506507508509510511512513514515516517518    {
        if (isset($this-&gt;_reflections[$class])) {
            return [$this-&gt;_reflections[$class], $this-&gt;_dependencies[$class]];
        }
 
        $dependencies = [];
        try {
            $reflection = new ReflectionClass($class);
        } catch (\ReflectionException $e) {
            throw new NotInstantiableException(
                $class,
                'Failed to instantiate component or class &quot;' . $class . '&quot;.',
                0,
                $e
            );
        }
 
        $constructor = $reflection-&gt;getConstructor();
        if ($constructor !== null) {
2. in /home/www/bernerfotos.de/vendor/yiisoft/yii2/di/Container.php at line 385– yii\di\Container::getDependencies('yii\web\cookie')
3. in /home/www/bernerfotos.de/vendor/yiisoft/yii2/di/Container.php at line 170– yii\di\Container::build('yii\web\cookie', [], ['name' =&gt; 'letzterBesuch', 'value' =&gt; '2022-08-14T15:41:21+02:00'])
4. in /home/www/bernerfotos.de/vendor/yiisoft/yii2/BaseYii.php at line 365– yii\di\Container::get('yii\web\cookie', [], ['name' =&gt; 'letzterBesuch', 'value' =&gt; '2022-08-14T15:41:21+02:00'])
5. in /home/www/bernerfotos.de/vendor/craftcms/cms/src/Craft.php at line 53– yii\BaseYii::createObject(['name' =&gt; 'letzterBesuch', 'value' =&gt; '2022-08-14T15:41:21+02:00'], [])
47484950515253545556575859     * @phpstan-param class-string&lt;T&gt;|array{class:class-string&lt;T&gt;}|callable():T $type
     * @param array $params
     * @return T
     */
    public static function createObject($type, array $params = [])
    {
        return parent::createObject($type, $params);
    }
 
    /**
     * Checks if a string references an environment variable (`$VARIABLE_NAME`)
     * and/or an alias (`@aliasName`), and returns the referenced value.
     *
6. in /home/www/bernerfotos.de/templates/index.html at line 179– Craft::createObject(['class' =&gt; 'yii\web\cookie', 'name' =&gt; 'letzterBesuch', 'value' =&gt; '2022-08-14T15:41:21+02:00'])
173174175176177178179180181 
        {# Besuchsdatum wird in einem Cookie gespeichert #}
        {# https://craftcms.stackexchange.com/questions/38195/show-content-since-last-visit #}
        {% set cookie = create({
            class: 'yii\\web\\cookie',
            name: 'letzterBesuch',
            value: now | atom,
        }) %}
        {% do craft.app.response.cookies.add(cookie) %}
7. in /home/www/bernerfotos.de/vendor/twig/twig/src/Template.php at line 394– __TwigTemplate_ba63bc429b431f800b3e497f230c9f24abbe035ef05bbc56b6f73d62467b2da8::doDisplay(['craft' =&gt; craft\web\twig\variables\CraftVariable, 'currentSite' =&gt; craft\models\Site, 'currentUser' =&gt; null, 'siteName' =&gt; 'Bernerfotos', ...], ['head' =&gt; [__TwigTemplate_ba63bc429b431f800b3e497f230c9f24abbe035ef05bbc56b6f73d62467b2da8, 'block_head'], 'header' =&gt; [__TwigTemplate_ba63bc429b431f800b3e497f230c9f24abbe035ef05bbc56b6f73d62467b2da8, 'block_header'], 'main' =&gt; [__TwigTemplate_ba63bc429b431f800b3e497f230c9f24abbe035ef05bbc56b6f73d62467b2da8, 'block_main']])
8. in /home/www/bernerfotos.de/vendor/twig/twig/src/Template.php at line 367– Twig\Template::displayWithErrorHandling(['craft' =&gt; craft\web\twig\variables\CraftVariable, 'currentSite' =&gt; craft\models\Site, 'currentUser' =&gt; null, 'siteName' =&gt; 'Bernerfotos', ...], ['head' =&gt; [__TwigTemplate_ba63bc429b431f800b3e497f230c9f24abbe035ef05bbc56b6f73d62467b2da8, 'block_head'], 'header' =&gt; [__TwigTemplate_ba63bc429b431f800b3e497f230c9f24abbe035ef05bbc56b6f73d62467b2da8, 'block_header'], 'main' =&gt; [__TwigTemplate_ba63bc429b431f800b3e497f230c9f24abbe035ef05bbc56b6f73d62467b2da8, 'block_main']])
9. in /home/www/bernerfotos.de/vendor/twig/twig/src/Template.php at line 379– Twig\Template::display([])
10. in /home/www/bernerfotos.de/vendor/twig/twig/src/TemplateWrapper.php at line 40– Twig\Template::render([], [])
11. in /home/www/bernerfotos.de/vendor/twig/twig/src/Environment.php at line 277– Twig\TemplateWrapper::render([])
12. in /home/www/bernerfotos.de/vendor/craftcms/cms/src/web/View.php at line 428– Twig\Environment::render('', [])
422423424425426427428429430431432433434 
        // Render and return
        $renderingTemplate = $this-&gt;_renderingTemplate;
        $this-&gt;_renderingTemplate = $template;
 
        try {
            $output = $this-&gt;getTwig()-&gt;render($template, $variables);
        } finally {
            $this-&gt;_renderingTemplate = $renderingTemplate;
            $this-&gt;setTemplateMode($oldTemplateMode);
        }
 
        $this-&gt;afterRenderTemplate($template, $variables, $templateMode, $output);
13. in /home/www/bernerfotos.de/vendor/craftcms/cms/src/web/View.php at line 481– craft\web\View::renderTemplate('', [])
475476477478479480481482483484485486487 
        $isRenderingPageTemplate = $this-&gt;_isRenderingPageTemplate;
        $this-&gt;_isRenderingPageTemplate = true;
 
        try {
            $this-&gt;beginPage();
            echo $this-&gt;renderTemplate($template, $variables);
            $this-&gt;endPage();
        } finally {
            $this-&gt;_isRenderingPageTemplate = $isRenderingPageTemplate;
            $this-&gt;setTemplateMode($oldTemplateMode);
            $output = ob_get_clean();
        }
14. in /home/www/bernerfotos.de/vendor/craftcms/cms/src/web/TemplateResponseFormatter.php at line 52– craft\web\View::renderPageTemplate('', [], 'site')
46474849505152535455565758        if (Craft::$app-&gt;getRequest()-&gt;getIsPreview()) {
            $view-&gt;registerAssetBundle(ContentWindowAsset::class);
        }
 
        // Render and return the template
        try {
            $response-&gt;content = $view-&gt;renderPageTemplate($behavior-&gt;template, $behavior-&gt;variables, $behavior-&gt;templateMode);
        } catch (Throwable $e) {
            if (!$e-&gt;getPrevious() instanceof ExitException) {
                // Bail on the template response
                $response-&gt;format = Response::FORMAT_HTML;
                throw $e;
            }
15. in /home/www/bernerfotos.de/vendor/yiisoft/yii2/web/Response.php at line 1095– craft\web\TemplateResponseFormatter::format(craft\web\Response)
16. in /home/www/bernerfotos.de/vendor/craftcms/cms/src/web/Response.php at line 286– yii\web\Response::prepare()
280281282283284285286287288289290291292 
    /**
     * @inheritdoc
     */
    protected function prepare(): void
    {
        parent::prepare();
        $this-&gt;_isPrepared = true;
    }
 
    /**
     * Clear the output buffer to prevent corrupt downloads.
     *
17. in /home/www/bernerfotos.de/vendor/yiisoft/yii2/web/Response.php at line 339– craft\web\Response::prepare()
18. in /home/www/bernerfotos.de/vendor/yiisoft/yii2/base/Application.php at line 390– yii\web\Response::send()
19. in /home/www/bernerfotos.de/web/index.php at line 26– yii\base\Application::run()
20212223242526define('CRAFT_ENVIRONMENT', getenv('ENVIRONMENT') ?: 'production');
// ...
 
// Load and run Craft
/** @var craft\web\Application $app */
$app = require CRAFT_VENDOR_PATH . '/craftcms/cms/bootstrap/web.php';
$app-&gt;run();
```

I cannot reproduce the error under the current desktop browsers Safari, Chrome and Firefox, here setting / querying the cookie works without problems.
With Microsoft Edge I get the same error message after deleting the cache and cookies?
</code></pre>
<p>I use the following code to query the cookie in the template</p>
<pre><code>{% set cookie = craft.app.request.cookies.get('letzterBesuch') %}
{% set letzterBesuch = cookie ? cookie.value : null %}

... some HTML-Code ...

{# Besuchsdatum wird in einem Cookie gespeichert #}
{# https://craftcms.stackexchange.com/questions/38195/show-content-since-last-visit #}
{% set cookie = create({
    class: 'yii\\web\\cookie',
    name: 'letzterBesuch',
    value: now | atom,
}) %}
        {% do craft.app.response.cookies.add(cookie) %}
</code></pre>
<p>I use the following versions</p>
<p>PHP version 8.0.21
OS version  Linux 4.19.0-17-amd64
Database driver &amp; version   MySQL 8.0.22
Image driver &amp; version  Imagick 3.6.0 (ImageMagick 6.9.10-23)
Craft edition &amp; version Craft Solo 4.2.1.1
Yii version 2.0.45
Twig version    v3.3.10
Guzzle version  7.4.5
Plug-ins</p>
<p>Redactor    3.0.2
SEO v4.0.3</p>
<p>I am not a developer / programmer, just a small Craft CMS user and would be very happy about tips and hints for troubleshooting.
Thank you once again for your help!</p>

<craft3><yii><cookies><craft4>
Not instantiable – yii\di\NotInstantiableException when setting and reading cookies
# Not instantiable – yii\di\NotInstantiableException when setting and reading cookies

(Asked by: username_0 on 2022-08-14)

Hello,
within a craft template I query the existence of a cookie to visually highlight new entries in the CMS with CSS. This worked for a long time without any problems.
A visitor told me that on Safari and iOS 15 an error message appears after deleting website data and history in Safari.
I could understand the effect on myself.
Not instantiable – yii\di\NotInstantiableException
Failed to instantiate component or class "yii\web\cookie".
Caused by: ReflectionException`


1. in /home/www/bernerfotos.de/vendor/yiisoft/yii2/di/Container.phpat line 509
500501502503504505506507508509510511512513514515516517518    {
        if (isset($this->_reflections[$class])) {
            return [$this->_reflections[$class], $this->_dependencies[$class]];
        }
 
        $dependencies = [];
        try {
            $reflection = new ReflectionClass($class);
        } catch (\ReflectionException $e) {
            throw new NotInstantiableException(
                $class,
                'Failed to instantiate component or class "' . $class . '".',
                0,
                $e
            );
        }
 
        $constructor = $reflection->getConstructor();
        if ($constructor !== null) {
2. in /home/www/bernerfotos.de/vendor/yiisoft/yii2/di/Container.php at line 385– yii\di\Container::getDependencies('yii\web\cookie')
3. in /home/www/bernerfotos.de/vendor/yiisoft/yii2/di/Container.php at line 170– yii\di\Container::build('yii\web\cookie', [], ['name' => 'letzterBesuch', 'value' => '2022-08-14T15:41:21+02:00'])
4. in /home/www/bernerfotos.de/vendor/yiisoft/yii2/BaseYii.php at line 365– yii\di\Container::get('yii\web\cookie', [], ['name' => 'letzterBesuch', 'value' => '2022-08-14T15:41:21+02:00'])
5. in /home/www/bernerfotos.de/vendor/craftcms/cms/src/Craft.php at line 53– yii\BaseYii::createObject(['name' => 'letzterBesuch', 'value' => '2022-08-14T15:41:21+02:00'], [])
47484950515253545556575859     * @phpstan-param class-string|array{class:class-string}|callable():T $type
     * @param array $params
     * @return T
     */
    public static function createObject($type, array $params = [])
    {
        return parent::createObject($type, $params);
    }
 
    /**
     * Checks if a string references an environment variable (`$VARIABLE_NAME`)
     * and/or an alias (`@aliasName`), and returns the referenced value.
     *
6. in /home/www/bernerfotos.de/templates/index.html at line 179– Craft::createObject(['class' => 'yii\web\cookie', 'name' => 'letzterBesuch', 'value' => '2022-08-14T15:41:21+02:00'])
173174175176177178179180181 
        {# Besuchsdatum wird in einem Cookie gespeichert #}
        {# https://craftcms.stackexchange.com/questions/38195/show-content-since-last-visit #}
        {% set cookie = create({
            class: 'yii\\web\\cookie',
            name: 'letzterBesuch',
            value: now | atom,
        }) %}
        {% do craft.app.response.cookies.add(cookie) %}
7. in /home/www/bernerfotos.de/vendor/twig/twig/src/Template.php at line 394– __TwigTemplate_ba63bc429b431f800b3e497f230c9f24abbe035ef05bbc56b6f73d62467b2da8::doDisplay(['craft' => craft\web\twig\variables\CraftVariable, 'currentSite' => craft\models\Site, 'currentUser' => null, 'siteName' => 'Bernerfotos', ...], ['head' => [__TwigTemplate_ba63bc429b431f800b3e497f230c9f24abbe035ef05bbc56b6f73d62467b2da8, 'block_head'], 'header' => [__TwigTemplate_ba63bc429b431f800b3e497f230c9f24abbe035ef05bbc56b6f73d62467b2da8, 'block_header'], 'main' => [__TwigTemplate_ba63bc429b431f800b3e497f230c9f24abbe035ef05bbc56b6f73d62467b2da8, 'block_main']])
8. in /home/www/bernerfotos.de/vendor/twig/twig/src/Template.php at line 367– Twig\Template::displayWithErrorHandling(['craft' => craft\web\twig\variables\CraftVariable, 'currentSite' => craft\models\Site, 'currentUser' => null, 'siteName' => 'Bernerfotos', ...], ['head' => [__TwigTemplate_ba63bc429b431f800b3e497f230c9f24abbe035ef05bbc56b6f73d62467b2da8, 'block_head'], 'header' => [__TwigTemplate_ba63bc429b431f800b3e497f230c9f24abbe035ef05bbc56b6f73d62467b2da8, 'block_header'], 'main' => [__TwigTemplate_ba63bc429b431f800b3e497f230c9f24abbe035ef05bbc56b6f73d62467b2da8, 'block_main']])
9. in /home/www/bernerfotos.de/vendor/twig/twig/src/Template.php at line 379– Twig\Template::display([])
10. in /home/www/bernerfotos.de/vendor/twig/twig/src/TemplateWrapper.php at line 40– Twig\Template::render([], [])
11. in /home/www/bernerfotos.de/vendor/twig/twig/src/Environment.php at line 277– Twig\TemplateWrapper::render([])
12. in /home/www/bernerfotos.de/vendor/craftcms/cms/src/web/View.php at line 428– Twig\Environment::render('', [])
422423424425426427428429430431432433434 
        // Render and return
        $renderingTemplate = $this->_renderingTemplate;
        $this->_renderingTemplate = $template;
 
        try {
            $output = $this->getTwig()->render($template, $variables);
        } finally {
            $this->_renderingTemplate = $renderingTemplate;
            $this->setTemplateMode($oldTemplateMode);
        }
 
        $this->afterRenderTemplate($template, $variables, $templateMode, $output);
13. in /home/www/bernerfotos.de/vendor/craftcms/cms/src/web/View.php at line 481– craft\web\View::renderTemplate('', [])
475476477478479480481482483484485486487 
        $isRenderingPageTemplate = $this->_isRenderingPageTemplate;
        $this->_isRenderingPageTemplate = true;
 
        try {
            $this->beginPage();
            echo $this->renderTemplate($template, $variables);
            $this->endPage();
        } finally {
            $this->_isRenderingPageTemplate = $isRenderingPageTemplate;
            $this->setTemplateMode($oldTemplateMode);
            $output = ob_get_clean();
        }
14. in /home/www/bernerfotos.de/vendor/craftcms/cms/src/web/TemplateResponseFormatter.php at line 52– craft\web\View::renderPageTemplate('', [], 'site')
46474849505152535455565758        if (Craft::$app->getRequest()->getIsPreview()) {
            $view->registerAssetBundle(ContentWindowAsset::class);
        }
 
        // Render and return the template
        try {
            $response->content = $view->renderPageTemplate($behavior->template, $behavior->variables, $behavior->templateMode);
        } catch (Throwable $e) {
            if (!$e->getPrevious() instanceof ExitException) {
                // Bail on the template response
                $response->format = Response::FORMAT_HTML;
                throw $e;
            }
15. in /home/www/bernerfotos.de/vendor/yiisoft/yii2/web/Response.php at line 1095– craft\web\TemplateResponseFormatter::format(craft\web\Response)
16. in /home/www/bernerfotos.de/vendor/craftcms/cms/src/web/Response.php at line 286– yii\web\Response::prepare()
280281282283284285286287288289290291292 
    /**
     * @inheritdoc
     */
    protected function prepare(): void
    {
        parent::prepare();
        $this->_isPrepared = true;
    }
 
    /**
     * Clear the output buffer to prevent corrupt downloads.
     *
17. in /home/www/bernerfotos.de/vendor/yiisoft/yii2/web/Response.php at line 339– craft\web\Response::prepare()
18. in /home/www/bernerfotos.de/vendor/yiisoft/yii2/base/Application.php at line 390– yii\web\Response::send()
19. in /home/www/bernerfotos.de/web/index.php at line 26– yii\base\Application::run()
20212223242526define('CRAFT_ENVIRONMENT', getenv('ENVIRONMENT') ?: 'production');
// ...
 
// Load and run Craft
/** @var craft\web\Application $app */
$app = require CRAFT_VENDOR_PATH . '/craftcms/cms/bootstrap/web.php';
$app->run();
```

I cannot reproduce the error under the current desktop browsers Safari, Chrome and Firefox, here setting / querying the cookie works without problems.
With Microsoft Edge I get the same error message after deleting the cache and cookies?

I use the following code to query the cookie in the template
{% set cookie = craft.app.request.cookies.get('letzterBesuch') %}
{% set letzterBesuch = cookie ? cookie.value : null %}

... some HTML-Code ...

{# Besuchsdatum wird in einem Cookie gespeichert #}
{# https://craftcms.stackexchange.com/questions/38195/show-content-since-last-visit #}
{% set cookie = create({
    class: 'yii\\web\\cookie',
    name: 'letzterBesuch',
    value: now | atom,
}) %}
        {% do craft.app.response.cookies.add(cookie) %}

I use the following versions
PHP version 8.0.21
OS version  Linux 4.19.0-17-amd64
Database driver & version   MySQL 8.0.22
Image driver & version  Imagick 3.6.0 (ImageMagick 6.9.10-23)
Craft edition & version Craft Solo 4.2.1.1
Yii version 2.0.45
Twig version    v3.3.10
Guzzle version  7.4.5
Plug-ins
Redactor    3.0.2
SEO v4.0.3
I am not a developer / programmer, just a small Craft CMS user and would be very happy about tips and hints for troubleshooting.
Thank you once again for your help!

**Tags:** craft3, yii, cookies, craft4

---

## 1 answers

### Answer by username_1 on 2022-08-14. Score: 2

So your code should actually be:
        {% set cookie = create({
            class: 'yii\\web\\Cookie',
            name: 'letzterBesuch',
            value: now | atom,
        }) %}

Notice the capitalized C in the yii\\web\\Cookie class namespace. Class names in PHP are (mostly) case-insensitive, auto-loaders such as used with Composer are case sensitive.
It's a best practice to use the correct casing regardless, IMO.
More: https://nystudio107.com/blog/cutting-the-cord-removing-plugins#plugin-2-cookies

> **Comments:**
> **username_0**: **Thank you so much!** Small cause with great effect … Is working!  

---

