简体   繁体   中英

Magento Module with Frontend and Admin functionality

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.

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

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 eg admin/mymodule

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM