Here We will know, what the current page, like CMS page or category view page or product view page or Homepage in Magento 2
Sometimes we need to check the current page so write or add conditions according to logic.
So the first step, to check the current page is to write the following script in the .phtml file or block and helper file
Where do you need to check which page is this?
Using object manager but not recammanded
$objectManager = \Magento\Framework\App\ObjectManager::getInstance(); $request = $objectManager->get('Magento\Framework\App\Action\Context')->getRequest(); Now you check your current page by the following code. if ($request->getFullActionName() == 'catalog_product_view') { //you are on the product page } if ($request->getFullActionName() == 'catalog_category_view') { //you are on the category page } if ($request->getFullActionName() == 'cms_index_index') { //you are on the home page } if ($request->getFullActionName() == 'cms_page_view') { //you are on the cms page }
Using di method
protected $request; public function __construct( \Magento\Framework\App\Request\Http $request, ) { $this->request = $request; } if ($this->request->getFullActionName() == 'catalog_product_view') { //you are on the product page } if ($this->request->getFullActionName() == 'catalog_category_view') { //you are on the category page } if ($this->request->getFullActionName() == 'cms_index_index') { //you are on the home page } if ($this->request->getFullActionName() == 'cms_page_view') { //you are on the cms page }
Like us on Facebook and Linkedin for more updates.
Related: Magento2 get category image