Site icon Sudhanshu's Blog

Magento 2 : How to get CMS page page_id from url ?

Magento 2 stores the URL identifier in url_rewrite table. You can get the page entity_id or page_id by idendifier. If you look into url_rewrite table where you can search by request_path column.

url_rewrite table

Programmatically you can use the following Model Class.

/<Magento Root Directory>/vendor/magento/module-cms/Model/Page.php

protected $_page;

public function __construct(
    ...
    \Magento\Cms\Model\Page $page,
    ...   
) {
    ...
    $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 with id : " . $page_id;
}else {
    echo "Page Not Found";
}

It will return 2 which is the default page id for home page.

Exit mobile version