1

Suppose I want to open contact page: {website}/contact, this page is also represented like {website}cms/page/view/id/12.

Is it possible to get somehow to get this url ({website}cms/page/view/id/12)? I'm trying to get it from request object, but without success.

UPD

Maybe I was to abstract. When I'm on some page: product, blog, cms, I need some unique value which will identify this page when I'll open it. Every time. I thought it can be such kind of url.

2
  • I don't think the contact page falls under the cms/page model?
    – Adam Moss
    Jun 11, 2013 at 13:01
  • It doesn't matter, call it thank-you page Jun 11, 2013 at 13:05

4 Answers 4

2

If I understand your post correctly you are looking to uniquely identify any given requested resource, not just CMS pages, right?
As you've found out, in most cases that works fine, except that for the CMS pages where the page_id param isn't added to the request path info.

The following should suffice in most cases, though you might want to customize it for layered navigation query arguments and such.

public function getRequestIdentifier()
{
    $req = $this->getRequest();
    $params = array('page_id', 'id', 'category', 'p', 'limit', 'order');
    $parts = array(
        $req->getModuleName(), $req->getControllerName(), $req->getActionName()
    );
    foreach ($params as $p) {
        $value = $req->getParam($p, false);
        if (false !== $value) {
            $parts[] = $p;
            $parts[] = $value;
        }
    }
    return implode('/', $parts);
}
2

For CMS pages, the route is the identifier. It is matched in the method Mage_Cms_Controller_Router::match(). This matching takes place after normal standard route matching. You can use many routes to get there:

This information is stored in the cms_page table.

0

The code (I think) you need is in Mage_Cms_Controller_Router::match() method.
Here is an adaptation of it.
First you get the requested identifier:

$request = Mage::app()->getRequest();//get the request: In the router class it is passed as a parameter but you can get it like this in your code
$identifier = trim($request->getPathInfo(), '/'); //the requested page identifier

Now check if there is a page with the requested identifier in the current store view.

$page   = Mage::getModel('cms/page');//Mage_Cms_Model_Page instance
$pageId = $page->checkIdentifier($identifier, Mage::app()->getStore()->getId());//check for page id
if ($pageId) {
    $rawUrl = 'cms/page/view/page_id/'.$pageId;
    //or full url
    $fullRawUrl = Mage::getUrl('cms/page/view', array('page_id'=>$pageId));
} 
else{
    //there is no cms page with this identifier.
}
0

If you're using Enterprise and Full Page Cache and need to get the target path from the request path, here is some code. Note that this assumes you add .html to your urls:

    $app = Mage::app();
    $request = $app->getRequest();
    $storeId = $app->getStore()->Id();
    $pathInfo = $request->getPathInfo();

    if (is_numeric(strpos($pathInfo, '.html'))){

        $requestPathWithHtml = substr($pathInfo, 1);
        $requestPathWithoutHtml = substr($pathInfo, 1, strpos($pathInfo, '.html') -  1);
        $rewriteCollection = Mage::getModel('enterprise_urlrewrite/url_rewrite')->getCollection();
        $rewriteCollection->addFieldToFilter('request_path', array('in'=>array($requestPathWithHtml, $requestPathWithoutHtml)));
        $rewriteCollection->addFieldToFilter('store_id', $storeId);

        $size = $rewriteCollection->getSize();
        if ($size>0){
            $rewrite = $rewriteCollection->getFirstItem();
            $pathInfo = $rewrite->getTargetPath();

        }
    }

    $explodedPath = explode('/', $pathInfo);
    foreach ($explodedPath as $i=>$part){
        if ($part == 'id'){
            $id = $explodedPath[$i+1];
        }
    }

This came while debugging Enterprise full page cache. First the page is generated normally, but block_html is set as false in Mage_Core_Model_Cache::_allowedCacheOptions. So your block is not saved in cache. After the page is generated, while FPC is filling the containers, the request pathInfo was not yet set (still the SEO url, i.e. '/product-name.html'). So, this lets you look up the target path.

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.