Magento – Magento 1.9- Pass a Parameter to cms page url

cms-pagesmagento-1.9url-key

I am new to Magento, I need to pass parameters to a cms page created through the backend under the cms section. The steps I followed to create a page are as follows:

  1. First of all, I added a new page layout using this answer.
  2. Then I added a new page from admin panel, and added the layout created in the previous step to this page.
  3. The page is created and I can access it using the url-key (custom-products) generated for this page, as example.com/custom-products The page content is loaded successfully.

Now my problem is, i want to pass a parameter to this url just like example.com/custom-products/1, but when I write /1 at the end of the url, It takes me to the Not found page.

But when I add a query-string parameters (example.com/custom-products?param=1) to this page the page works fine. But I don't want the parameter passed as query-string. I need it to be as example.com/custom-products/1

Any help in this regard would be really appreciating.

Bundle of thanks in advance.

Best Answer

You can't use following type of url for cms page

example.com/custom-products/1 

OR

example.com/custom-products/param/1

Because CMS Router class take whole things as an identifier. In example it's create identifier for url like

custom-products/1

AND

custom-products/param/1

But when you create CMS page, you assign identifier 'custom-products' not custom-products/1 or custom-products/param/1, That's why it load nothing. Check following class

app/code/core/Mage/Cms/Controller/Router.php

/**
 * Validate and Match Cms Page and modify request
 *
 * @param Zend_Controller_Request_Http $request
 * @return bool
 */
public function match(Zend_Controller_Request_Http $request)
{
    if (!Mage::isInstalled()) {
        Mage::app()->getFrontController()->getResponse()
            ->setRedirect(Mage::getUrl('install'))
            ->sendResponse();
        exit;
    }

    $identifier = trim($request->getPathInfo(), '/');

    $condition = new Varien_Object(array(
        'identifier' => $identifier,
        'continue'   => true
    ));
    Mage::dispatchEvent('cms_controller_router_match_before', array(
        'router'    => $this,
        'condition' => $condition
    ));
    $identifier = $condition->getIdentifier();

    if ($condition->getRedirectUrl()) {
        Mage::app()->getFrontController()->getResponse()
            ->setRedirect($condition->getRedirectUrl())
            ->sendResponse();
        $request->setDispatched(true);
        return true;
    }

    if (!$condition->getContinue()) {
        return false;
    }

    $page   = Mage::getModel('cms/page');
    $pageId = $page->checkIdentifier($identifier, Mage::app()->getStore()->getId());
    if (!$pageId) {
        return false;
    }

    $request->setModuleName('cms')
        ->setControllerName('page')
        ->setActionName('view')
        ->setParam('page_id', $pageId);
    $request->setAlias(
        Mage_Core_Model_Url_Rewrite::REWRITE_REQUEST_PATH_ALIAS,
        $identifier
    );

    return true;
}

Check following line


$page   = Mage::getModel('cms/page');
$pageId = $page->checkIdentifier($identifier, Mage::app()->getStore()->getId());
if (!$pageId) {
    return false;
}

How to overcome?

Create your own router and make your own way.

How you create Router?

You use an example of CMS module. Open config.xml and check following code sample


<events>
    <controller_front_init_routers>
        <observers>
            <cms>
                <class>Mage_Cms_Controller_Router</class>
                <method>initControllerRouters</method>
            </cms>
        </observers>
    </controller_front_init_routers>
</events>

Create your router class like Vendor_Module_Controller_Router.