I am trying to create a simple module based on this answer https://magento.stackexchange.com/a/125519/45358

I just need a function to check if customer is logged or not in a custom theme header.phtml. I am new to Magento 2 and I can't understand how this works. I tried by following some tutorials but I am getting a lot of errors, like class not found and many more. I just need a complete example and then I will understand how actually work.

Here all files in my module





namespace George\Core\Block\Userlogin;

class Login extends Magento\Framework\View\Element\Template
    public function isCustomerLoggedIn()
   return $this->_session->isLoggedIn();



namespace George\Core\Model\Index;

class Index extends \Magento\Framework\App\Action\Action
   protected $_session;

public function __construct(
\Magento\Customer\Model\Session $session,

) {
$this->_session = $session; 



<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd">
<module name="George_Core" setup_version="0.0.1"/>

Also I found that need to add somewhere a preference, I understand that preference should be inside /etc/di.xml but how the actual di.xml file should look?

 <preference for="Block\That\Renders\The\Template"
        type="Vendor\Module\Block\Your\Custom\Block" />

Also in my case I can't understand which is Block\That\Renders\The\Template and Vendor\Module\Block\Your\Custom\Block

Now how can I call this inside custom theme header.phtml template, because if I try that, website stops loading.

if ($block->isCustomerLoggedIn()) {
// Customer is logged in
} else {
// Customer is not logged in

If someone can provide me a simple complete example will be very helpful.

1 Answer 1


Your di.xml should look like this ;

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
 <preference for="Block\That\Renders\The\Template"
        type="Vendor\Module\Block\Your\Custom\Block" />

Also Block\That\Renders\The\Template refers to the path of the block that renders the module. You can know the path of the block that renders the module by its corresponding xml. Lets take example of store_switcher block ,since it is located in all pages its xml is default.xml located in app/design/frontend/Magento/luma/Magento_Theme/layout/default.xml for luma theme .

Its reference container looks like ..

<referenceContainer name="footer">
     <block class="Magento\Store\Block\Switcher" name="store_switcher" as="store_switcher" after="footer_links" template="switch/stores.phtml"/>

So its corresponding phtml is switch/stores.phtml and block that renders it is Magento\Store\Block\Switcher

  • Thanks for your answer, but how can I call module on my custom theme? Apr 4, 2017 at 12:45
  • You will have to make the function you're calling i.e isCustomerLoggedIn() in your block and call it in your phtml using $block->isCustomerLoggedIn() . Apr 4, 2017 at 12:52
  • I am getting an error Exception #0 (Magento\Framework\Exception\LocalizedException): Invalid method ....::isCustomerLoggedIn Apr 4, 2017 at 12:57
  • Try to var_dump ($this->_session);exit; to check if it is even available in your block . If no you have to inject instance of Magento\Customer\Model\Session . Apr 4, 2017 at 13:50
  • If i place that layout is going broken, everything inside <head></head> not loading Apr 4, 2017 at 14:12

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.