How To

How to Get User Information Inside of Controller in Magento 2

Hello Magento Friends,

Today I am here with the new topic, How to Get User Information Inside of Controller in Magento 2. Have you reviewed our previously published blog? If not, check it out now. How To Disable System Configuration Field Programmatically in Magento 2.

Controllers are an important part of MVC flow. They are responsible for a specific URL or group of URLs. Magento 2 admin can get user data inside the controller. User sessions can be obtained inside the controller to redirect customers who are not logged in. Let’s see how it works.

Steps to Get User Information Inside of Controller in Magento 2:

Step 1: Move to the below path

app\code\Vendor\Extension\Controller\Index\Custom.php

And, add this code

<?php
namespace Vendor\Extension\Controller\Index;

class Custom extends \Magento\Framework\App\Action\Action 
{
    protected $_customerSession;
    
    protected $_customerUrl;

    public function __construct(
        \Magento\Framework\App\Action\Context $context,
        \Magento\Customer\Model\Session $customerSession,
        \Magento\Customer\Model\Url $customerUrl
    ) {
        $this->_customerSession = $customerSession;
        $this->_customerUrl = $customerUrl;
        parent::__construct($context);
    }

    public function isCustomerLogin()
    {
        return $this->_customerSession->isLoggedIn();
    }

    public function getCustomerLoginUrl() 
    {   
        return $this->_customerUrl->getLoginUrl();
    }
}

That’s it!

Conclusion:

This way you can Get User Information Inside of Controller in Magento 2. In case of any issues, reach me via the comment box. See you again with the next solution, till then stay connected with us!

Happy Coding!

Click to rate this post!
[Total: 4 Average: 5]
Dhiren Vasoya

Dhiren Vasoya is a Director and Co-founder at MageComp, Passionate 🎖️ Certified Magento Developer👨‍💻. He has more than 9 years of experience in Magento Development and completed 850+ projects to solve the most important E-commerce challenges. He is fond❤️ of coding and if he is not busy developing then you can find him at the cricket ground, hitting boundaries.🏏

Recent Posts

How to Integrate ChatGPT with Laravel Application?

In this guide, we'll explore how to integrate ChatGPT, an AI-powered chatbot, with a Laravel…

2 days ago

What are Net Sales? How to Calculate Your Net Sales?

In the world of business, understanding financial metrics is crucial for making informed decisions and…

4 days ago

Magento 2 Extensions Digest April 2024 (New Release & Updates)

Welcome to the MageComp Monthly Digest, where we bring you the latest updates, releases, and…

4 days ago

The ABCs of Geofencing: Definition, Features and Uses

In this era, businesses are always on the lookout for ways to engage with their…

5 days ago

How to Delete Product Variant in a Shopify Remix App using GraphQL Mutations?

Managing a Shopify store efficiently involves keeping your product catalog organized. This includes removing outdated…

6 days ago

6 Innovative Tools Revolutionizing E-Commerce Operations

E-commerce has transformed the way consumers shop for products and services and interact with businesses.…

1 week ago