How to get current category name by current category url in observer magento2

I want to get category name by current url in the observer. I have tried most common code but not working, so I am trying to get category name or id by current url. For example, current url is : http://example.php/food.html. Here I want category id by this url.

I have tried this in my observer :

namespace Mymodule\Event\Observer\Logger;
use Magento\Framework\Registry;

class GlobalLogger implements ObserverInterface
{
    protected $registry;

    public function __construct(
        \Magento\Framework\Registry $registry
) {
        $this->registry = $registry;
    }

    public function execute(\Magento\Framework\Event\Observer $observer)
    { 
        $objectManager = \Magento\Framework\App\ObjectManager::getInstance();

        $category = $objectManager->get('Magento\Framework\Registry')->registry('current_category');//get current category
        echo $category->getId();
        echo $category->getName();
    }
}

Anyone have an idea for that, please?

Solutions

You just need to check first whether current request is from category page and if from category then you need to call below code,

namespace Mymodule\Event\Observer\Logger;
use Magento\Framework\Registry;
class GlobalLogger implements ObserverInterface
{
    protected $registry;
    protected $request;

    public function __construct(
        \Magento\Framework\Registry $registry,
        \Magento\Framework\App\Request\Http $request
    ) {
        $this->registry = $registry;
        $this->request = $request;
    }

    public function execute(\Magento\Framework\Event\Observer $observer)
    { 
        if($this->getRequest()->getActionName() == 'category'){ 
            $category = $this->registry->registry('current_category');//get current category
            echo $category->getId();
            echo $category->getName();
        }
     }
 }

Remove var and generated folder from root and check again.

Try below code:

<?php
/**
 * Copyright © 2013-2017 Magento, Inc. All rights reserved.
 * See COPYING.txt for license details.
 */
namespace Mymodule\Event\Observer\Logger;

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

/**
 * Class GlobalLogger
 */
class GlobalLogger implements ObserverInterface
{

    /**
    * @param \Magento\Framework\Registry $registry
    */

    protected $registry;
    protected $request;

    public function __construct(
        \Magento\Framework\App\RequestInterface $request,
        \Magento\Framework\Registry $registry
    ) {
        $this->request      = $request;
        $this->registry = $registry;
    }

    /**
     * Set new customer group to all his quotes
     *
     * @param Observer $observer
     * @return void
     */
    public function execute(Observer $observer)
    {
        $category = $this->registry->registry('current_category');
        if($category) {
          echo $category->getName();
          die;
        }
    }
}

After run below compiler command

php bin/magento setup:di:compile

Tags: Magento 2.x

Similar questions

Get Current URL in Magento including sub-pages of that URL and show something
I already know how to get a specific URL in Magento and show something. It was already resolved in this page: https://stackoverflow.com/questions/25192212/get-current-url-in-magento-and-show-something However, I noticed that it will only show what I want to show in the page alone. If I browse through its subpages, it doesn't show anymore. For examp...
Magento2 How to get Store specific Category Id In custom page by using Category URL If same URL exist in both the stores?
I am having the same category in 2 stores and on the custom page, I need category detail as per current store id. I am using the below code to get a category by using the category URL. In block public function getCategoryId() In phtml $categoryId = $block->getCategoryId()->getFirstItem()->getEntityId(); Now I am having 2 stores. How to get...
Redirect to specific controller from observer without changing my current browser Url
I had written a observer to trap product view page event and it is working fine.Now in that code i want to call a controller and pass the product id in it and render the custom view.phtml file, so how can i call that controller without changing my current browser url and render the view.phtml file on same url. My Observer Code is Given Below
Redirect to custom module controller and render themplate from observer without changing my current browser Url
I had written a observer to trap product view page event and it is working fine.Now i just want to load block from custom module controller without changing my current browser url so for that i am calling initForward() function with my custom module details but it is not working .The code for the try out is given below.
Get category/product url (URL rewrite url) and re-direct after login
I want to Get category/product url (URL rewrite url) and re-direct customer to that url after login. I tried getLastUrl from session / redirectReferrer. Both returns only the actual url and not the SEO ones. Is there any other possibility to achieve this on a common way. I will be using this redirect throughout the site on all pages, not only in PD...
How could I get the attribute(brand) Url Like example.com/category/brand/name in magento2
Please give me some Idea,I need some help. I fetch the brands from those attribute..and get the ids from those attribute. Ans can send those data on the URL like example.com/category?brand=7 but I don't want this..

Also ask

We use cookies to deliver the best possible experience on our website. By continuing to use this site, accepting or closing this box, you consent to our use of cookies. To learn more, visit our privacy policy.