4

I'm trying to create very simple thing - when user goes to URL http://example.com/brands/brand_name it should show a brand page that should be created based on .phtml template.

I have already added a router and successfully matched the URL:

class My_Module_Controller_Router extends Mage_Core_Controller_Varien_Router_Abstract
{

    public function match(Zend_Controller_Request_Http $request) 
    {
        // let's assume that URL is http://example.com/brands/adidas

        $pageId = $request->getPathInfo(); // "brands/adidas"
        $params = trim($pageId, '/'); 
        $params = explode('/', $params); // array(brands, adidas)

        if($params[0] === 'brands') {

            // Here I need somehow to create brand page based on custom template (.phtml file that I already built)

            return true;
        }

    }

}

Please help me to understand how to tell Magento to print the template phtml file that I need.

Thank you!

  • Thanks for that. But i have an issue. I have tried the code above, when i try to reach /brands/ page a normal page with my site header and footer appear but empty, same if i try to reach /brand/ and if i try to reach /brands/adidas same. Any idea on what is wrong ? I use magento 1.7.0.2 Tom – user2627 Jul 9 '13 at 15:36
8

You should redirect the the request to one of your controller actions. From there you can use the default Magento layout/block system to present the data you want the customer to see.
Here is what worked for me.
app/etc/modules/Easylife_Brand.xml - declaration file

<?xml version="1.0"?>
<config>
    <modules>
        <Easylife_Brand>
            <active>true</active>
            <codePool>local</codePool>
            <depends>
                <Mage_Core />
            </depends>
        </Easylife_Brand>
    </modules>
</config>

app/code/local/Easylife/Brand/etc/config.xml - configuration file

<?xml version="1.0"?>
<config>
    <modules>
        <Easylife_Brand>
            <version>0.0.1</version>
        </Easylife_Brand>
    </modules>
    <frontend>
        <routers>
            <brand>
                <use>standard</use>
                <args>
                    <module>Easylife_Brand</module>
                    <frontName>brand</frontName>
                </args>
            </brand>
        </routers>
        <layout>
            <updates>
                <easylife_brand>
                    <file>easylife_brand.xml</file>
                </easylife_brand>
            </updates>
        </layout>
    </frontend>
    <global>
        <events>
            <controller_front_init_routers>
                <observers>
                    <brand>
                        <class>Easylife_Brand_Controller_Router</class>
                        <method>initControllerRouters</method>
                    </brand>
                </observers>
            </controller_front_init_routers>
        </events>
    </global>
</config>

app/code/local/Easylife/Brand/Controller/Router.php - custom router.

<?php
class Easylife_Brand_Controller_Router extends Mage_Core_Controller_Varien_Router_Abstract{
    public function initControllerRouters($observer){
        $front = $observer->getEvent()->getFront();
        $front->addRouter('brand', $this);
        return $this;
    }
    public function match(Zend_Controller_Request_Http $request){
        if (!Mage::isInstalled()) {
            Mage::app()->getFrontController()->getResponse()
                ->setRedirect(Mage::getUrl('install'))
                ->sendResponse();
            exit;
        }
        $pathInfo = trim($request->getPathInfo(), '/');
        $params = explode('/', $pathInfo);
        if(isset($params[0]) && $params[0] == 'brands') {
             //redirect to mymodule/brands/index/brand_name/addidas
             $request->setModuleName('brand')  
                     ->setControllerName('index')
                     ->setActionName('index');
            if (isset($params[1])){
                 $request->setParam('brand_name', $params[1]);
            }
            $request->setAlias(
                Mage_Core_Model_Url_Rewrite::REWRITE_REQUEST_PATH_ALIAS,
                $pathInfo
            );
            return true;
        }
        return false;
    }
}

app/code/local/Easylife/Brand/controllers/IndexController.php - the controller

<?php
class Easylife_Brand_IndexController extends Mage_Core_Controller_Front_Action{
    public function indexAction(){
        $brandName = $this->getRequest()->getParam('brand_name', null);
        $this->loadLayout();
        $block = $this->getLayout()->getBlock('brands');//get the block instance
        if ($block){ //if block is valid
             if (is_null($brandName)){//if no brand name is supplied
                 $block->setTemplate('easylife_brand/no_brand.phtml');
             }
             else{
                 $block->setTemplate('easylife_brand/adidas.phtml');
             }
        }
        $this->renderLayout();
    }
}

app/design/frontend/base/default/layout/easylife_brand.xml - layout file

<?xml version="1.0"?>
<layout>
    <brand_index_index>
        <reference name="content">
            <block type="core/template" name="brands" as="brands" />
        </reference>
    </brand_index_index>
</layout>

app/design/frontend/base/default/template/easylife_brand/no_brand.html - demo template shown when no brand parameter is passes.

<div>
No brand selected:<br /><br />
List of brands:<br />
Adidas<br />
Nike<br />
</div>

app/design/frontend/base/default/template/easylife_brand/adidas.html - demo template shown when a brand parameter is passes.

<div>This is the adidas brand page.</div>

Clear the cache.
Now when I call mysite.com/brands/ I get the first template (the one with 'No brand selected'.
If I call mysite.com/brands/addidas or mysite.com/brands/something I get the second template. ('this is the adidas brand page').

  • Thank you as always for help. For some weird reason, when I add "return true;" inside that if statement (in "match" function), I receive "Front controller reached 100 router match iterations" PHP error. Otherwise if I add "return false;" - I get 404. I disabled all plugins and commented everything to be sure that other code isn't causing this, weird... – Marvin3 Jun 22 '13 at 14:28
  • I never had this issue with a custom router. Is your module called 'brands'? or something similar? Maybe the router matching is running an infinite loop. – Marius Jun 22 '13 at 15:01
  • Nope, there is nothing even close to word "brand" in name of module. I'm including Router in the same way as this plugin does - github.com/wware-dev/store/blob/… (through event in config.xml). Maybe I should add it differently? Thank you. – Marvin3 Jun 22 '13 at 15:49
  • Seams like you are doing it right. – Marius Jun 22 '13 at 15:55
  • I've just tested the code and I get the same result. I will update the answer when I get a solution. – Marius Jun 22 '13 at 16:32

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.