0

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<T>|array{class:class-string<T>}|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!

1 Answer 1

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

1
  • Thank you so much! Small cause with great effect … Is working! Aug 15, 2022 at 6:50

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.