5

How can I retrieve the Identifier ( URL key ) of current page in Magento 2?

6 Answers 6

5

Copy this code in any phtml file to get current cms page id and identifier in magento2:

<?php

$objectManagerCms = \Magento\Framework\App\ObjectManager::getInstance();
$cmsPage = $objectManagerCms->get('\Magento\Cms\Model\Page');

echo $cmsPage->getIdentifier(); //Current CMS Page Identifier

echo $cmsPage->getId(); //Current CMS Page ID

?>
4

you can get the page id like this:

$pageId = $this->_request->getParam('page_id', $this->_request->getParam('id', false));

if you are not in a controller, you need to inject an instance of \Magento\Framework\App\RequestInterface in your class.

You also need to inject an instance of \Magento\Cms\Api\PageRepositoryInterface in your class and then you can call:

$page = $this->pageRepository->getById($pageId);
$url = $page->getIdentifier();

So, your class can look like this:

namespaece Your\Namespace\Here;

class SomeClassName extends SomethingElse 
{
    protected $_request; //only if this is not a controller
    protected $pageRepository;
    public function __construct(
        ...
        \Magento\Framework\App\RequestInterface $request,
        \Magento\Cms\Api\PageRepositoryInterface $pageRepository,
        ....
    )  { 
        ....
        $this->_request = $request; //only if not in a controller
        $this->pageRepository = $pageRepository;
        ....
    }
    public function getCurrentPageUrlKey()
    {
        try {
           $pageId = $this->_request->getParam('page_id', $this->_request->getParam('id', false));
           $page = $this->pageRepository->getById($pageId);  
           return $page->getIdentifier();
        } catch (\Magento\Framework\Exception\NoSuchEntityException $e) {
            return null;
        }
    }
}
2
  • Thank you for the help I got it working by following method public function getIdentifier() { try { $objectManager = \Magento\Framework\App\ObjectManager::getInstance(); $configHelper = $objectManager->create('\Magento\Cms\Api\PageRepositoryInterface'); $pageId = $this->_request->getParam('page_id', $this->_request->getParam('id', false)); $page = $configHelper->getById($pageId); return $page->getIdentifier(); } catch (\Magento\Framework\Exception\NoSuchEntityException $e) { return null; } }
    – nuwaus
    Commented May 16, 2016 at 19:01
  • 1
    even if it works, using the object manager is not a recommended practice.
    – Marius
    Commented May 16, 2016 at 19:57
1

You can use in Helper like this:

namespace Your\Namespace\Helper;

class Data extends \Magento\Framework\App\Helper\AbstractHelper{

protected $objectManager;

public function __construct(
    \Magento\Framework\ObjectManagerInterface $objectManager,
){
    $this->objectManager = $objectManager;
}

public function getCurrentPageIdentifier() {

   $_cmsPage = $this->objectManager->get('\Magento\Cms\Model\Page');
   $_cmsPageURL = $_cmsPage->getIdentifier();
   return $_cmsPageURL;
}

}

And call in phtml:

$_Helper = $this->helper('Your\Namespace\Helper\Data');

$_pageIdentifier = $_Helper->getCurrentPageIdentifier();

echo $_pageIdentifier;

Hope this can help your.

0

If you want only the slug.

$currentUrl = $this->getUrl('*/*/*', ['_current' => true, '_use_rewrite' => true]);
$urlParts = explode("/", $currentUrl);
$slug = end($urlParts);
-1

You can use following to get current page Identifier :

$this->getRequest()->getRouteName()
2
-2

If you need the query string (the bit after the ? in a URL), that part is in this variable:

$_SERVER['QUERY_STRING']
1
  • this won't work if you have an url rewrite. For example if a page has the url key page but you have an url rewrite from page1 to page and you call the url page1 your code will return page1 instead of page.
    – Marius
    Commented May 16, 2016 at 12:15

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.