Magento 2 – How to Get URL Data by Request Path

magento2url-rewrite

I have gathered URLs from sitemap.xml now I need category ID against category URL and Product ID against product URL. I want to do it by getting URL data from url_rewrite table

In Magento 1.9 we use this code to get data by request path

Mage::getModel('core/url_rewrite')->loadByRequestPath("abcPath");

How can we do it in Magento 2?

Best Answer

After some digging in magento core code I finally got solution.

I have used this code to get url_rewrite data by url key

/* Find url data by request path */
$filterData = [
    UrlRewrite::REQUEST_PATH => $path
];
$rewrite = $this->urlFinder->findOneByData($filterData);

$type = $rewrite->getEntityType();
$entityId = $rewrite->getEntityId();
$targetPath = $rewrite->getTargetPath();

Hope it will help others too.

Related Topic