0

I have created events.xml in app/code/Demo/Mymodule/etc/frontend:

<?xml version="1.0">
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Event/etc/events.xsd">
    <event name="layout_load_before">
        <observer name="add_layout_handles" instance="Demo\Mymodule\Observer\AddHandles" />
    </event>
</config>

And AddHandles.php in app/code/Demo/Mymodule/Observer:

<?php

namespace Demo\Mymodule\Observer;

use Magento\Framework\Event\Observer;
use Magento\Framework\Event\ObserverInterface;
use Magento\Customer\Model\Session as CustomerSession;

class AddHandles implements ObserverInterface
{
    protected $customerSession;
    public function __construct(CustomerSession $customerSession)
    {
        $this->customerSession = $customerSession;
    }

    public function execute(\Magento\Framework\Event\Observer $observer)
    {
        $layout = $observer->getEvent()->getLayout();


        if (!$this->customerSession->isLoggedIn())
        {
            $layout->getUpdate()->addHandle('customer_logged_out');
        }
    }
}

On running php bin/magento setup:di:compile getting this error: arse error: syntax error, unexpected 'public' (T_STRING ), expecting function (T_FUNCTION) or const (T_CONST)

Can anyone help me?

I am using Magento 2.2.4 version.

2 Answers 2

0

Try this. You forgot to declare @var

<?php

namespace Demo\Mymodule\Observer;

use Magento\Framework\Event\Observer;
use Magento\Framework\Event\ObserverInterface;
use Magento\Customer\Model\Session as CustomerSession;

class AddHandles implements ObserverInterface
{
    /**
    * @var CustomerSession
    */
    protected $customerSession;
    /**
     * Add constructor.
     * @param CustomerSession $customerSession
     */
    public function __construct(CustomerSession $customerSession)
    {
        $this->customerSession = $customerSession;
    }

    public function execute(\Magento\Framework\Event\Observer $observer)
    {
        $layout = $observer->getEvent()->getLayout();


        if (!$this->customerSession->isLoggedIn())
        {
            $layout->getUpdate()->addHandle('customer_logged_out');
        }
    }
}

Events.XML

<?xml version="1.0" encoding="UTF-8"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../../../../../../lib/internal/Magento/Framework/Event/etc/events.xsd">
    <event name="layout_load_before">
        <observer name="add_layout_handles" instance="Demo\Mymodule\Observer\AddHandles" />
    </event>
</config>
6
  • Thanks for the response. But still facing same issue. Jul 19, 2018 at 15:21
  • Did you flush the cache? Jul 19, 2018 at 15:23
  • Yes I have already flushed the cache Jul 19, 2018 at 15:25
  • Try my updated code Jul 19, 2018 at 15:29
  • Your previous code works, however now I am getting this error: app\/code\/Demo\/Mymodule\/etc\/frontend\/events.xml:\nInput is not proper UTF-8 I have checked from var/report Jul 19, 2018 at 15:44
0

Try this solution, it might be work for you.

events.xml

<?xml version="1.0" encoding="UTF-8"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../../../../../../lib/internal/Magento/Framework/Event/etc/events.xsd">
    <event name="layout_load_before">
        <observer name="add_layout_handles" instance="Demo\Mymodule\Observer\AddHandles" />
    </event>
</config>

AddHandles.php

<?php

namespace Demo\Mymodule\Observer;

use Magento\Framework\Event\Observer;
use Magento\Framework\Event\ObserverInterface;

class AddHandles implements ObserverInterface
{
    /**
     * @var \Magento\Framework\App\Http\Context
     */
    protected $_context;

    /**
     * Add constructor.
     * @param \Magento\Framework\App\Http\Context $context
     */
    public function __construct(\Magento\Framework\App\Http\Context $context)
    {
        $this->_context = $context;
    }

    public function execute(\Magento\Framework\Event\Observer $observer)
    {
        $layout = $observer->getEvent()->getLayout();


        if (!$this->_context->getValue(\Magento\Customer\Model\Context::CONTEXT_AUTH))
        {
            $layout->getUpdate()->addHandle('customer_logged_out');
        }
    }
}

This CONST (\Magento\Customer\Model\Context::CONTEXT_AUTH) use for checking the customer is logged in or not.

Your Answer

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

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