0

I simply want to get the CMS page id from a random url.

So i have a page with the url "Foo/Bar", and I can get the page id for that page using :

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

But now i want to have the page_id of "Foo". but ofcourse that is not requested so I can't use the getParam method.

What method should I use to get the page ID from a page where I only know the url of?

2

You can use the url_rewrite table. Where you can search by request_path and use entity_id as your page Id.

Or try this if works for you.

protected $_page;

public function __construct(
    ...
    \Magento\Cms\Model\Page $page,
    ...
    array $data = []
) {
    parent::__construct($context, $data);
    ...
    $this->_page = $page;
    ...
}
    /**
     * Check if page identifier exist for specific store return page id if page exists
     *
     * @param string $identifier
     * @param int $storeId
     * @return int
     */
$page_id = $this->_page->checkIdentifier('home', 0);
if ($page_id > 0) {
    echo "Page Found";
}else {
    echo "Page Not Found";
}
| improve this answer | |
  • I'd assume there is a more elegant solution for this problem, isn't there? – Litsher - Nathan Feb 3 at 10:19
  • 1
    @Litsher-Nathan Updated. Please have a look. – Sudhanshu Bajaj Feb 3 at 10:24
  • 1
    You're a legend! Thanks man! :) – Litsher - Nathan Feb 3 at 10:28

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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