[英]Call method from (custom) controller class in Magento
I'm working with the M2e extension for Magento. 我正在使用Magento的M2e扩展。 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
. 现在,我要在文件
app/code/community/Ess/M2ePro/controllers/Adminhtml/ListingController.php
调用类Ess_M2ePro_Adminhtml_ListingController
的方法。
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.
也许直接调用此控制器方法不是一个好主意,但就我而言(将关联的magento产品删除到ebay列表中),需要调用此方法。
In general these actions are called from the magento backend. 通常,这些动作是从magento后端调用的。 I've also tried to create an admin_html session, but at the moment I don't have any further ideas.
我也尝试过创建一个admin_html会话,但是目前我没有任何进一步的想法。
Here's an example, how it looks like. 这是一个例子,看起来像什么。 I'm working with regular PHP-code, nothing special:
我正在使用常规的PHP代码,没什么特别的:
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. 在您的情况下,特别是在方法末尾
exit
时。
You can use the _forward
method if you are in a controller: 如果您在控制器中,则可以使用
_forward
方法:
$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. 但是最干净的方法是将所需的代码放入帮助器中,并在两个控制器中从该帮助器调用代码。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.