Category Controller overriding to allow multiple Categories in URL
-
12-04-2021 - |
Question
My custom module (AKA AraujoPhillips/SpecialCategories
) should allow URLs with multiple categories, like:
example.com/category1/category2.html
example.com/sales/phones.html
The following custom router manage these kind of URLs (Which is working):
AraujoPhillips/SpecialCategories/Controller/Router.php
public function match(Zend_Controller_Request_Http $request)
{
$this->request = $request;
$this->realUri = /* Some code here */;
$this->mainCategoryPath = /* Some code here */;
if ($this->isSpecialCategoryUri($this->realUri)) {
$urlRewrite = Mage::getModel('core/url_rewrite');
$urlRewrite->setStoreId(Mage::app()->getStore()->getId())
->loadByRequestPath($this->mainCategoryPath);
$this->request->setPathInfo($this->mainCategoryPath);
$this->request->setModuleName('catalog');
$this->request->setControllerName('category');
$this->request->setActionName('view');
$this->request->setParam(
'id',
$urlRewrite->getCategoryId()
);
$urlRewrite->rewrite($this->request);
return true;
}
return false;
}
/**
* Eval if the category allows more categories in URL
* @param string
* @return boolean
*/
private function isSpecialCategoryUri($uri){
// Do something
}
Everything looks good at this point.
Then, I'm trying to extend CategoryController.php
from Mage_Catalog_CategoryController
:
AraujoPhillips/SpecialCategories/etc/config.xml
<config>
<modules>
<AraujoPhillips_SpecialCategories>
<version>0.1.0</version>
</AraujoPhillips_SpecialCategories>
</modules>
<global>
...
<events>
<controller_front_init_routers>
<observers>
<araujophillips_specialcategories>
...
</araujophillips_specialcategories>
</observers>
</controller_front_init_routers>
</events>
</global>
<frontend>
<routers>
<catalog>
<args>
<modules>
<AraujoPhillips_SpecialCategories before="Mage_Catalog">AraujoPhillips_SpecialCategories</AraujoPhillips_SpecialCategories>
</modules>
</args>
</catalog>
</routers>
</frontend>
</config>
And finally:
AraujoPhillips/SpecialCategories/controllers/CategoryControllers.php
require_once Mage::getModuleDir('controllers','Mage_Catalog').DS.'CategoryController.php';
class AraujoPhillips_SpecialCategories_CategoryController extends Mage_Catalog_CategoryController
{
public function viewAction()
{
echo 'It worked!!';
die();
}
}
The message 'It worked!!'
is not being shown so the CategoryController.php
file is not being extended.
Where is the error?
Solution 2
Everything was OK!!!
The controller wasn't being overridden because another extension (Amasty_Shopby
in my case) is also overriding it!
So, I applied the following modification to my config.xml:
AraujoPhillips/SpecialCategories/etc/config.xml
<config>
<modules>
<AraujoPhillips_SpecialCategories>
<version>0.1.0</version>
</AraujoPhillips_SpecialCategories>
</modules>
<global>
...
</global>
<frontend>
<routers>
<catalog>
<args>
<modules>
<AraujoPhillips_SpecialCategories before="Amasty_Shopby">AraujoPhillips_SpecialCategories</AraujoPhillips_SpecialCategories>
</modules>
</args>
</catalog>
</routers>
</frontend>
</config>
I also added the modules dependency in app/etc/modules XML file:
app/etc/modules/AraujoPhillips_SpecialCategories.xml
<config>
<modules>
<AraujoPhillips_SpecialCategories>
<active>true</active>
<codePool>local</codePool>
<depends>
<Amasty_Shopby />
</depends>
</AraujoPhillips_SpecialCategories>
</modules>
</config>
OTHER TIPS
You should be focusing on rewrites not routers and controllers. Also, there’s an option in the store configurations to add category paths to URLs.