Magento Module with Frontend and Admin functionality

梦想的初衷 提交于 2019-12-05 16:08:12

Depending upon the area(frontend or adminhtml), frontend or adminhtml router are dispatched.
So you need not need to worry about getting it messed up as long as you are using different controller files for frontend and adminhtml, frontend controller extending from Mage_Core_Controller_Front_Action & adminhtml extending from Mage_Adminhtml_Controller_Action.

Frontend / Adminhtml routers can be defined as (just a syntax):

<frontend>
    <routers>
        <[module]>
            <use>standard</use>
            <args>
                <module>[Namespace]_[Module]</module>
                <frontName>[module]</frontName>
            </args>
        </[module]>
    </routers>
</frontend>
<admin>
    <routers>
        <[module]>
            <use>admin</use>
            <args>
                <module>[Namespace]_[Module]</module>
                <frontName>[module]</frontName>
            </args>
        </[module]>
    </routers>
</admin>

And you can create frontend controllers under: app/code/[codePool]/[Namespace]/[Module]/controllers/
For example:

<?php
//file: app/code/local/MagePsycho/Testmodule/controllers/IndexController.php
class MagePsycho_Testmodule_IndexController extends Mage_Core_Controller_Front_Action
{
    public function indexAction(){

    }
}

In order to access it from url: http://your-magento-url/testmodule/index/index
and adminhtml controllers under: app/code/[codePool]/[Namespace]/[Module]/controllers/Adminhtml/
For example:

<?php
//file: app/code/local/MagePsycho/Testmodule/controllers/Adminhtml/IndexController.php
class MagePsycho_Testmodule_Adminhtml_IndexController extends Mage_Adminhtml_Controller_Action
{
    public function indexAction(){

    }
}


In order to access it from url: http://your-magento-url/testmodule/adminhtml_index/index
(You can see the Adminhtml folder for separating adminhtml controllers)

Hope this gave you some info.
Thanks

Zifius

Have a look at my similar question: Admin route in custom modules

I also would recommend using

<admin>
 <routers>
   <adminhtml>
     <args>
       <modules>
         <modulename before="Mage_Adminhtml">Namespace_Module_Adminhtml</modulename>
       </modules>
     </args>
   </adminhtml>
 </routers>
</admin>

This will allow you to avoid using adminhtml part in the routes, so your module backend url will have simple and clean url like core modules e.g. admin/mymodule

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!