Php 使用magento创建一个没有索引的好url
我想制作一个phtml文件,显示特定品牌的产品 url需要如下所示:Php 使用magento创建一个没有索引的好url,php,magento,url-rewriting,module,rewrite,Php,Magento,Url Rewriting,Module,Rewrite,我想制作一个phtml文件,显示特定品牌的产品 url需要如下所示: brands/Subway brands/Mcdonalds brands/Lego brands/Barbie brands/Duplo brands/..... 在品牌是一个变量的情况下,如何使此url工作 我曾尝试制作一个模块,但在这种情况下,我会得到一个类似brands/index/brands/Subway的url 我也不想重写每个品牌(100+)您可以在模块中创建一个自定义路由器来处理诸如品牌/Subway之类的
brands/Subway
brands/Mcdonalds
brands/Lego
brands/Barbie
brands/Duplo
brands/.....
在品牌是一个变量的情况下,如何使此url工作
我曾尝试制作一个模块,但在这种情况下,我会得到一个类似brands/index/brands/Subway的url
我也不想重写每个品牌(100+)您可以在模块中创建一个自定义路由器来处理诸如
品牌/Subway
之类的路由这是你需要的 首先,您需要让页面在不重写的情况下工作。
让我们假设在这个url
mysite.com/brands/index/index
-您拥有品牌列表在url
mysite.com/brands/index/view/id/12
,您拥有id为12的品牌
在config.xml中,您需要将其添加到
标记中:
<events>
<controller_front_init_routers>
<observers>
<[namespace]_[module]>
<class>[Namespace]_[Module]_Controller_Router</class>
<method>initControllerRouters</method>
</[namespace]_[module]>
</observers>
</controller_front_init_routers>
</events>
就这样。清除缓存并尝试一下。您可以在.htaccess(根目录中的一个)中创建重写规则 如果您的url为: www.yoursite.com/brands/index/brands?brand=MyBrandName 您将看到: www.yoursite.com/brand/MyBrandName
希望有帮助 我不能让它工作。现在url是/merk/index/view/name/Subway。我在Router.php中与observer
SpeelgoedwinkelXL\u Brands\u Controller\u Router initControllerRouters
进行了一次全局搜索,我将函数IntitControllerRouters更改为echo“testing”;出口但它不会在屏幕上显示任何内容。或者无法调试路由器?对不起,我的坏消息…我忘记了一个xml标记
。我已经更新了代码
<?php
class [Namespace]_[Module]_Controller_Router extends Mage_Core_Controller_Varien_Router_Abstract{
public function initControllerRouters($observer){
$front = $observer->getEvent()->getFront();
$front->addRouter('[module]', $this);
return $this;
}
public function match(Zend_Controller_Request_Http $request){
if (!Mage::isInstalled()) {
Mage::app()->getFrontController()->getResponse()
->setRedirect(Mage::getUrl('install'))
->sendResponse();
exit;
}
$urlKey = trim($request->getPathInfo(), '/');
$check = array();
$parts = explode('/', $urlKey);
if ($parts[0] == 'brands'){//if url key starts with 'brands'
if (!isset($parts[1])){ //if the url is just 'brands' - then redirect to brand list
return false;//do nothing...the request will be handled by the default router and you will see `brands/index/index/`.
}
elseif ($parts[1] == 'index'){//if your controller is named differently change 'index' to your controller name
return false; //do nothing...the request will be handled by the default router and you will see `brands/index/index/`.
}
else {
//if a name is specified
//get the entities with that name.
$collection = Mage::getModel('[module]/[entity]')->getCollection()
//if the field is named differently change 'name' to your field name.
->addAttributeToFilter('name', $parts[1]);
$collection->getSelect()->limit(1);//limit to only one
$itemId = (int)$collection->getFirstItem()->getId();
//redirect to 'brands/index/view/id/$itemId'
$request->setModuleName('brands')
->setControllerName('index') //if your controller is named differently change 'index' to your controller name
->setActionName('view') //if your action is named differently change 'view' to action name
->setParam('id', $itemId);
$request->setAlias(
Mage_Core_Model_Url_Rewrite::REWRITE_REQUEST_PATH_ALIAS,
$urlKey
);
return true;
}
}
return false;
}
}
Options +FollowSymLinks
RewriteEngine on
RewriteRule ^brand/(.+)$ /brands/index/brands?brand=$1 [L,R=301,QSA]