3

In a page template (.phtml) file, we can generate a link to another CMS page using the following snippet:

<a href="<?php echo $this->getUrl('my_page_url_key');?>">Link to my page</a>

But is it possible to have a link generated if we know the ID of the page? Example:

<a href="<?php echo $this->getUrl(3);?>">Link to my page with ID 3</a>

This way the admin can change url key without breaking the theme links.

2
  • 1
    @Rakesh Not at all. I'm not asking for the current page! There is a hint in your answer, though: $page = $this->pageRepository->getById($pageId); $url = $page->getIdentifier();which I will try. Oct 26, 2016 at 15:40
  • @Rakesh No, the block I'm using doesn't implement the pageReposity class. I could overload the class with my own, I suppose, but that's overkill for just one link. Oct 26, 2016 at 15:46

1 Answer 1

15

By using object manager you can get CMS page url, if you have already id of cms page.

$pageId = 1;
$this->_objectManager = \Magento\Framework\App\ObjectManager::getInstance();

or 

$this->_objectManager = $block->_objectManager;
$CMSPageURL = $this->_objectManager->create('Magento\Cms\Helper\Page')
->getPageUrl($pageId);

you can call object manager in your block using

protected $_objectManager;
public function __construct( 
... 
\Magento\Framework\ObjectManagerInterface $objectmanager, 
... ) { 
      ... 
      $this->_objectManager = $objectmanager; 
      ... 
}

You can also use a helper like this, as this can be used without needing the objectManager:

$pageHelper = $this->helper('Magento\Cms\Helper\Page');
$pageHelper->getPageUrl('my-page-id');

May be it will help you, thanks.

6
  • This works! :) But I'm worried about using the objectManager like that, as I've read that it's discouraged? Oct 26, 2016 at 15:47
  • if you have used custom block then you can call block variable of object manager, like $block->_objectManager in phtml; public function __construct(\Magento\Framework\View\Element\Template\Context $context, array $data = array()) { $this->_objectManager = \Magento\Framework\App\ObjectManager::getInstance(); parent::__construct($context, $data); } or public function __construct( ... \Magento\Framework\ObjectManagerInterface $objectmanager, ... ) { ... $this->_objectManager = $objectmanager; ... } Oct 27, 2016 at 4:38
  • 1
    Thank you, @chirag I'm marking your reply as the answer. I will use your suggestion as it's the best one I've seen so far, and doesn't look that offensive. I'd be happy for someone to explain when it's okay to use the object manager, and when it's not! Oct 27, 2016 at 12:28
  • 1
    For everyone who thinks this is a good example - it's not. Instead of declaring object manager in the constructor - declare object that you want to call, in this case 'Magento\Cms\Helper\Page'. Moreover - do not call/declare objectmanager if you do not know how it is need to be used.
    – Macas
    Jun 25, 2018 at 15:56
  • 1
    You can also declare "Magento\Cms\Helper\Page" this class in block constructor. Jun 26, 2018 at 7:10

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