XML-Rewrite for frontend only
-
16-10-2019 - |
문제
In my custom news-module I got this:
<rewrite>
<fancy_url>
<from><![CDATA[/news\/(.+)/]]></from>
<to><![CDATA[news/index/view/title/$1/]]></to>
<complete>1</complete>
</fancy_url>
</rewrite>
under the <global>
section atm.
How can I set this rewrite, to only rewrite frontend?
Edit (Answer 1): I already use this:
<admin>
<routers>
<news>
<use>admin</use>
<args>
<module>Centano_News</module>
<frontName>news</frontName>
</args>
</news>
</routers>
</admin>
<adminhtml>
<menu>
<news module="news">
<title>News</title>
<sort_order>71</sort_order>
<children>
<items module="news">
<title>Manage News</title>
<sort_order>0</sort_order>
<action>news/adminhtml_news</action>
</items>
</children>
</news>
</menu>
And when I change it like you said, it redirects me always to the dashboard when I click "Manage News". May I miss something here :-/
Edit (Answer 2): I thought I could avoid own router. I will try it!
해결책
This is an addition to Sander's answer. You should definitely do what he says.
Also you can add your own router to match urls like news/something-here
.
For this create a new class in app/code/[codepool]/[Namespace]/News/Controller/Router.php
- Replace [codepool]
and [Namespace]
with the appropriate values.
<?php
class [Namespace]_News_Controller_Router extends Mage_Core_Controller_Varien_Router_Abstract {
protected $_restrictedNames = array('index'); //add here other values that should not be matched. I mean don't match urls like `news/index`
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(), '/');
$parts = explode('/', $urlKey);
//check if the url is 'news/something' and the second part in not in the restricted array
if (count($parts) == 2 && $parts[0] == 'news' && !in_array($parts[0], $this->_restrictedNames)){
$request->setModuleName('news')
->setControllerName('index')
->setActionName('view')
->setParam('title', $parts[1]);
$request->setAlias(
Mage_Core_Model_Url_Rewrite::REWRITE_REQUEST_PATH_ALIAS,
$urlKey
);
return true;
}
return false;
}
public function initControllerRouters($observer){
$front = $observer->getEvent()->getFront();
$front->addRouter('news', $this);
return $this;
}
}
Now add in the config.xml
of the module inside the <global>
tag, this:
<events>
<controller_front_init_routers>
<observers>
<news>
<class>[Namespace]_News_Controller_Router</class>
<method>initControllerRouters</method>
</news>
</observers>
</controller_front_init_routers>
</events>
And comment out the xml section you posted in the question.
다른 팁
Please consider using the following XML for routing the backend of your extension
...
<admin>
<routers>
<!-- Includes our controller, so when we add the adminhtml menu item below, it is found! -->
<adminhtml>
<args>
<modules>
<[module] before="Mage_Adminhtml">[Namespace]_[Module]_Adminhtml</[module]>
</modules>
</args>
</adminhtml>
</routers>
</admin>
<adminhtml>
<menu>
<[module] module="[module]">
<title>[Module]</title>
<sort_order>71</sort_order>
<children>
<items module="[module]">
<title>Manage Items</title>
<sort_order>0</sort_order>
<action>adminhtml/[module]</action>
</items>
</children>
</[module]>
</menu>
...
(taken from the Magento Wiki)
This way your extension will start with /admin/
and won't be rewritten