Magento Module with Frontend and Admin functionality

為{幸葍}努か 提交于 2019-12-07 07:08:48

问题


I'm currently working on a custom module for Magento. I understand the basics of Packages, Modules and Routers, and I have built the front end part of my module.

However I am now moving on to the admin side of things. However I'm a little bit confused by how I add the admin part to my routers and get it to call the relevant controller.

Let's imagine I have created these routers...

<frontend>
    <routers>
        <slider>
            <use>standard</use>
            <args>
                <module>Mypackage_Myodule</module>
                <frontName>Mymodule</frontName>
            </args>
        </slider>
    </routers> 
</frontend> 
<admin>
    <routers>
        <mymoduleadmin>
            <use>admin</use>
            <args>
                <module>Mypackage_Myodule</module>
                <frontName>Mymodule</frontName>
            </args>
        </mymoduleadmin>
    </routers>
</admin>

I presume that both these routers will attempt to call controllers/IndexController.php and therefore the same functionality? Is it possible to set things up so my routers call different controllers depending on whether they are front end or admin? Is this even possible or do I need to set up a front end module and an admin module?

I apologise if this is a School Boy question, but this has me a bit confused and in reality I just want to know the most efficient way to set up a custom module with front end and admin functionality.


回答1:


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




回答2:


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



来源:https://stackoverflow.com/questions/11792962/magento-module-with-frontend-and-admin-functionality

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