Php 自定义Slug Magento
我在为我的博客模块创建自定义路由时遇到了一些问题(我这样做是为了更好地学习Magento) 到目前为止我所做的: 创建了一个自定义表,其中还包含所需的段塞。 在my config.xml中添加了以下内容Php 自定义Slug Magento,php,magento,routes,magento-1.7,router,Php,Magento,Routes,Magento 1.7,Router,我在为我的博客模块创建自定义路由时遇到了一些问题(我这样做是为了更好地学习Magento) 到目前为止我所做的: 创建了一个自定义表,其中还包含所需的段塞。 在my config.xml中添加了以下内容 <default> <web> <!-- what does this represent??? --> <routers> <namespace_blog_list>
<default>
<web> <!-- what does this represent??? -->
<routers>
<namespace_blog_list>
<area>frontend</area>
<class>Namespace_Blog_Controller_Router</class>
</namespace_blog_list>
</routers>
</web>
</default>
PS:$post->checkIdentifier返回以下数组:
array(
'controller' => 'index',
'action' => 'index',
'params' => array(
'id' => 3
)
)
问题是它是无限循环的,我发现了以下报告:
a:5:{i:0;s:52:"Front controller reached 100 router match iterations";i:1;s:337:"#0 /var/www/app/code/core/Mage/Core/Controller/Varien/Front.php(183): Mage::throwException('Front controlle...')
#1 /var/www/app/code/core/Mage/Core/Model/App.php(354): Mage_Core_Controller_Varien_Front->dispatch()
#2 /var/www/app/Mage.php(683): Mage_Core_Model_App->run(Array)
#3 /var/www/index.php(87): Mage::run('', 'store')
#4 {main}";s:3:"url";s:21:"/index.php/blog/index";s:11:"script_name";s:10:"/index.php";s:4:"skin";s:7:"default";}`
有什么想法吗?这是最好的方法,还是有其他推荐的解决方案
谢谢,CMS页面路由是在控制器前端初始化路由器事件的帮助下添加的 首先,您需要为模块定义标准前端路由
<config>
<frontend>
<routers>
<mymodule>
<use>standard</use>
<args>
<module>My_Module</module>
<frontName>mymodule</frontName>
</args>
</mymodule>
</routers>
</frontend>
</config>
在途中,你们应该检查标识符是否符合你们的需要,然后打电话给你们的管制员
class My_Module_Controller_Router extends Mage_Core_Controller_Varien_Router_Abstract
{
/**
* Inject new route into the list of routes
*
* @param Varien_Event_Observer $observer
*/
public function initControllerRouters($observer)
{
$front = $observer->getEvent()->getFront();
$route = new My_Module_Controller_Router();
$front->addRouter('myroute', $route);
}
/**
* Compare current path with the route rules
*
* @param Zend_Controller_Request_Http $request
* @return boolean
*/
public function match(Zend_Controller_Request_Http $request)
{
if (!Mage::app()->isInstalled()) {
Mage::app()->getFrontController()->getResponse()
->setRedirect(Mage::getUrl('install'))
->sendResponse();
return FALSE;
}
$route = 'myroute';
$identifier = $request->getPathInfo();
if (substr(str_replace("/", "", $identifier), 0, strlen($route)) == $route) {
if (str_replace("/", "", $identifier) == $route) {
$request->setModuleName('mymodule')
->setControllerName('index')
->setActionName('index');
return TRUE;
}
$suffix = Mage::helper('catalog/product')->getProductUrlSuffix();
$identifier = substr_replace($request->getPathInfo(), '', 0, strlen("/" . $route . "/"));
$identifier = str_replace($suffix, '', $identifier);
// here we make some check to make sure we have requested page
$mymodule = Mage::getModel('mymodule/mymodule');
$module_id = $mymodule->checkIdentifier($identifier, Mage::app()->getStore()->getId());
if (!$module_id) {
return FALSE;
}
// send request to the module's controller
$request->setModuleName('mymodule')
->setControllerName('index')
->setActionName('view')
->setParam('id', $module_id);
return TRUE;
} else {
return FALSE;
}
}
}
就这些
<default>
<web>
<default>
<cms_home_page>home</cms_home_page>
<cms_no_route>no-route</cms_no_route>
<cms_no_cookies>enable-cookies</cms_no_cookies>
<front>cms</front>
<no_route>cms/index/noRoute</no_route>
<show_cms_breadcrumbs>1</show_cms_breadcrumbs>
</default>
</web>
</default>
家
没有路线
启用Cookie
cms
cms/索引/路由
1.
这是cms模块的默认扇区。您可以在“管理”中的“系统-配置-web-默认页面”下找到这些设置您可以参考Alan Storm的文章,详细了解magento中的路由是如何工作的。非常感谢您花时间帮助我,事实上我对magento中的路由系统的工作原理有了更好的了解。我查阅了大多数艾伦·斯道姆的教程,但这是我不完全理解的少数几件事之一
class My_Module_Controller_Router extends Mage_Core_Controller_Varien_Router_Abstract
{
/**
* Inject new route into the list of routes
*
* @param Varien_Event_Observer $observer
*/
public function initControllerRouters($observer)
{
$front = $observer->getEvent()->getFront();
$route = new My_Module_Controller_Router();
$front->addRouter('myroute', $route);
}
/**
* Compare current path with the route rules
*
* @param Zend_Controller_Request_Http $request
* @return boolean
*/
public function match(Zend_Controller_Request_Http $request)
{
if (!Mage::app()->isInstalled()) {
Mage::app()->getFrontController()->getResponse()
->setRedirect(Mage::getUrl('install'))
->sendResponse();
return FALSE;
}
$route = 'myroute';
$identifier = $request->getPathInfo();
if (substr(str_replace("/", "", $identifier), 0, strlen($route)) == $route) {
if (str_replace("/", "", $identifier) == $route) {
$request->setModuleName('mymodule')
->setControllerName('index')
->setActionName('index');
return TRUE;
}
$suffix = Mage::helper('catalog/product')->getProductUrlSuffix();
$identifier = substr_replace($request->getPathInfo(), '', 0, strlen("/" . $route . "/"));
$identifier = str_replace($suffix, '', $identifier);
// here we make some check to make sure we have requested page
$mymodule = Mage::getModel('mymodule/mymodule');
$module_id = $mymodule->checkIdentifier($identifier, Mage::app()->getStore()->getId());
if (!$module_id) {
return FALSE;
}
// send request to the module's controller
$request->setModuleName('mymodule')
->setControllerName('index')
->setActionName('view')
->setParam('id', $module_id);
return TRUE;
} else {
return FALSE;
}
}
}
<default>
<web>
<default>
<cms_home_page>home</cms_home_page>
<cms_no_route>no-route</cms_no_route>
<cms_no_cookies>enable-cookies</cms_no_cookies>
<front>cms</front>
<no_route>cms/index/noRoute</no_route>
<show_cms_breadcrumbs>1</show_cms_breadcrumbs>
</default>
</web>
</default>