简体   繁体   中英

Call method from (custom) controller class in Magento

I'm working with the M2e extension for Magento. Now I want to call a method of the class Ess_M2ePro_Adminhtml_ListingController in the file app/code/community/Ess/M2ePro/controllers/Adminhtml/ListingController.php .

But I don't know, how. I can't create an object or model to get access to the class to use the methods. Maybe it's not a good idea to call this controller methods directly, but in my case (remove a associated magento product to an ebay listing) it's required to call this methods.

In general these actions are called from the magento backend. I've also tried to create an admin_html session, but at the moment I don't have any further ideas.

Here's an example, how it looks like. I'm working with regular PHP-code, nothing special:

class Ess_M2ePro_Adminhtml_ListingController extends Ess_M2ePro_Controller_Adminhtml_MainController
{
    //#############################################

    protected function _initAction()
    {
        /** removed **/
    }

    protected function _isAllowed()
    {
        return Mage::getSingleton('admin/session')->isAllowed('m2epro/listings/listing');
    }

    //#############################################

    public function indexAction()
    {
     /** removed **/
    }

    //#############################################

    public function searchAction()
    {
       /** removed **/
    }

    public function searchGridAction()
    {
       /** removed **/
    }


    public function lockListingNowAction()
    {
        $listingId = (int)$this->getRequest()->getParam('id');
        $component = $this->getRequest()->getParam('component');

        $lockItemParams = array(
            'id' => $listingId,
            'component' => $component
        );

        $lockItem = Mage::getModel('M2ePro/Listing_LockItem',$lockItemParams);

        if (!$lockItem->isExist()) {
            $lockItem->create();
        }

        exit();
    }




}  

And I'm looking for something like this:

$test = Mage::getModel('M2ePro/Ess_M2ePro_Adminhtml_ListingController')->lockListingNowAction();

You shouldn't call methods from an other controller. Specially in your case, when you have exit at the end of the method.
You can use the _forward method if you are in a controller:

$this->_forward($action = 'lockListingNowAction', $controller = 'adminhtml_listing', $module = 'M2ePro', $params = array('id'=>$id)) //controller name may be different

But the cleanest way is to have the code you need in a helper and call the code from that helper in both controllers.

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