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.
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!
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!
Hello Magento Friends, In today’s blog, I will explain How to Add Tooltip in Checkout…
MongoDB is a popular NoSQL database that offers flexibility and scalability when handling modern web…
In NodeJS, callbacks empower developers to execute asynchronous operations like reading files, handling requests, and…
Hello Magento Friends, In today’s blog, we will learn How to Show SKU in Order…
The "Buy Now" and "Add to Cart" buttons serve as the primary call-to-action (CTA) elements…
Hello Magento Friends, In Magento 2, the checkout process allows customers to choose multiple shipping…