How can I retrieve the Identifier ( URL key ) of current page in Magento 2?
6 Answers
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
?>
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;
}
}
}
-
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; } }
– nuwausCommented May 16, 2016 at 19:01 -
1even if it works, using the object manager is not a recommended practice.– Marius ♦Commented May 16, 2016 at 19:57
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.
If you want only the slug.
$currentUrl = $this->getUrl('*/*/*', ['_current' => true, '_use_rewrite' => true]);
$urlParts = explode("/", $currentUrl);
$slug = end($urlParts);
You can use following to get current page Identifier :
$this->getRequest()->getRouteName()
-
this doesn't always work: magento.stackexchange.com/questions/115435/…– Marius ♦Commented May 16, 2016 at 12:24
-
If you need the query string (the bit after the ? in a URL), that part is in this variable:
$_SERVER['QUERY_STRING']
-
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 frompage1
topage
and you call the urlpage1
your code will returnpage1
instead ofpage
.– Marius ♦Commented May 16, 2016 at 12:15