扩展/覆盖Magento中的扩展 [英] Extending / overriding extension in Magento
问题描述
我安装了一个扩展,我想从我的模块中使用它的功能.该扩展中的postAction就是所有发生的地方.它使用youtube API检索视频信息,并将其保存在Magento EAV数据模型的多个表上.
我已经创建了一个功能模块,用于仅通过按钮和文本框发送一些搜索词来测试youtube API函数.但是现在我想使用扩展功能来自动执行该调用并填写必要的表,而不是手动执行我的代码中的所有操作.
因此,我需要(或想要?还是必须?)来设置对该postAction的调用,或者扩展或覆盖它.我在这里迷路了,我是Magento和PHP的新手,所以我不清楚该怎么做.
这是我要呼叫的班级:
/**
* Youtube Upload Controller
*/
class AW_Vidtest_YoutubeController extends Mage_Core_Controller_Front_Action {
.....
}
其中包含postAction函数:
/**
* Get customer video
*/
public function postAction() {
$data = new Varien_Object($this->getRequest()->getPost());
....
}
我已经阅读了这些链接上的信息,但是我不清楚我必须做什么.遵循Observer
模式?也许自己创建一个post
调用,然后以某种方式添加$data
结构,以便可以在该调用中使用它?谢谢
已
这是我到目前为止所拥有的代码,并附有@Francesco的建议.函数printVideoEntry
是从其他函数中调用的,每个函数中的printVideoEntry
现在将遍历目录中的前3个产品.
<?php
class Dts_Videotestimonials_Model_SearchVideo extends Mage_Core_Model_Abstract
{
public $search_term;
private $productModel;
function printVideoEntry($videoEntry, $_product, $tabs = "")
{
# get user data
$user = Mage::getSingleton('admin/session');
$userName = $user->getUser()->getFirstname();
$userEmail = $user->getUser()->getEmail();
$data = array(
"ProductId" => $_product->getId(),
"AuthorEmail" => $userEmail,
"AuthorName" => $userName,
"VideoLink" => $videoEntry->getVideoWatchPageUrl(),
"VideoType" => "link",
"Title" => $videoEntry->getVideoTitle(),
"Comment" => "this is a comment"
);
$actionUrl = Mage::getUrl('vidtest/youtube/post');
Mage::app()->getResponse()->setRedirect($actionUrl, $data);
}
}
要给出明确的答案并不容易...问题尚不清楚,因为我们不知道youtube扩展程序是如何工作的. (代码是加密的还是开放的?)
调用控制者的操作
如果只想调用postAction,则可以使用_redirect($path, $arguments=array())
方法. (在Mage/Core/Controller/Varien/Action.php
中定义)
-
$path
被定义为'moduleName/controllerName' -
$arguments=array()
定义为一对parameterName => Value.
例如.
$this->_redirect('checkout/cart', array('Pname' => $pValue, ... );
这仅在您从控制器调用时才有效...
您可以在此处找到有关_redirect的更多信息:>具有以下参数的magento _redirect +或/
如果要从模型或控制器的任何其他文件中进行重定向,则需要以这种方式调用url:
Mage::app()->getResponse()->setRedirect(Mage::getUrl($path, $arguments=array()));
所以上面的ex.成为:
Mage::app()->getResponse()->setRedirect(Mage::getUrl('checkout/cart', array('Pname' => $pValue, ... ));
观察者
使用观察者意味着向您的模块(观察者)添加一个新模型,并在此类内编写一个在某些事件下执行操作的方法,可能您想调用yt扩展的某些模型/方法.
然后,您必须在config.xml中声明这些东西,将您的观察者方法绑定到某个事件(甚至在Magento中适合您的任何预定义,或者如果需要,您应该创建自己的重写magento类...)>
观察者示例
-
PackageName/ModuleName/Model/Observer.php
class PackageName_ModuleName_Model_Observer { public function myActionMethod(Varien_Event_Observer $observer) { // Code ... // just a very indicative example $model = Mage::getModel('youtubeExtension/Model....').method(); } }
-
PackageName/ModuleName/etc/config.xml
<config> .... <global> <events> <EventName> <observers> <moduleName_observer> <type>singleton</type> <class>PackageName_ModuleName_Model_Observer</class> <method>myActionMethod</method> </moduleName_observer> </observers> </EventName> </events> </global> ....
显然要根据您的程序包/模块/方法名称更改EventName
和所有假名称
最困难的是找到适合您的合适活动...
每当您在magento代码中看到诸如Mage::dispatchEvent('EventName', Parameters);
之类的东西时,这都是一个事件.
您可以在默认的Magento事件列表中找到此处
希望对您有帮助
I have an extension installed and I want to use its funcionality from my modules. The postAction in that extension is where all happens. It uses youtube API to retrieve a video information and save it on several tables on the Magento EAV data model.
Already have a functional module that I created to test youtube API functions using just a button and a text box to send some search term. But now I want to do it automatically using the extension funcionalities to make that call and fill in the necessary tables instead of doing everything manually from my code.
So I need (or want? or must?) to setup a call to that postAction or extend or override it. I'm lost here, I'm new to Magento and PHP so I haven´t a clear idea on what to do.
This is the class I want to call:
/**
* Youtube Upload Controller
*/
class AW_Vidtest_YoutubeController extends Mage_Core_Controller_Front_Action {
.....
}
And inside it the postAction function:
/**
* Get customer video
*/
public function postAction() {
$data = new Varien_Object($this->getRequest()->getPost());
....
}
I have read the information on these links but I'm not clear on what exactly I'm must do. Follow the Observer
pattern? Maybe just creating a post
call by myself and somehow adding the $data
structure so it can be used on the call? Thanks
- How do I overwrite/extend an abstract class?
- avoiding extension conflicts
- block override from two different modules
- overriding magento bloc in multiple modules
Edited:
This is the code I have until now, with suggestions made by @Francesco. The function printVideoEntry
is called from other function, inside a for each that for now walks the first 3 products on the catalog.
<?php
class Dts_Videotestimonials_Model_SearchVideo extends Mage_Core_Model_Abstract
{
public $search_term;
private $productModel;
function printVideoEntry($videoEntry, $_product, $tabs = "")
{
# get user data
$user = Mage::getSingleton('admin/session');
$userName = $user->getUser()->getFirstname();
$userEmail = $user->getUser()->getEmail();
$data = array(
"ProductId" => $_product->getId(),
"AuthorEmail" => $userEmail,
"AuthorName" => $userName,
"VideoLink" => $videoEntry->getVideoWatchPageUrl(),
"VideoType" => "link",
"Title" => $videoEntry->getVideoTitle(),
"Comment" => "this is a comment"
);
$actionUrl = Mage::getUrl('vidtest/youtube/post');
Mage::app()->getResponse()->setRedirect($actionUrl, $data);
}
}
It is not easy to give a clear answer ... the question is not clear because we don't know how the youtube extension works. ( the code is crypted or open ? )
Call a Controller's Action
If you want to just call postAction you can use _redirect($path, $arguments=array())
method. ( defined in Mage/Core/Controller/Varien/Action.php
)
$path
is defined as 'moduleName/controllerName'$arguments=array()
are defined as couple parameterName => Value.
Ex.
$this->_redirect('checkout/cart', array('Pname' => $pValue, ... );
This will work only if you call it from a Controller ...
you can find more info about _redirect here: magento _redirect with parameters that have + or /
In case you want to do a redirection from a model or any different file form a Controller one you will need to call the url in this way :
Mage::app()->getResponse()->setRedirect(Mage::getUrl($path, $arguments=array()));
so the above ex. becames:
Mage::app()->getResponse()->setRedirect(Mage::getUrl('checkout/cart', array('Pname' => $pValue, ... ));
Observer
Using an Observer means add a new model to your module ( the observer ) and write inside this class a method that perform an action under certain events, probably you want to calls some model/method of the yt extension.
Then you have to declare this stuff in you config.xml binding you observer method to some event ( any predefined even in Magento that suit you or if you need you should create your own rewriting the magento class ... )
Example for Observer
PackageName/ModuleName/Model/Observer.php
class PackageName_ModuleName_Model_Observer { public function myActionMethod(Varien_Event_Observer $observer) { // Code ... // just a very indicative example $model = Mage::getModel('youtubeExtension/Model....').method(); } }
PackageName/ModuleName/etc/config.xml
<config> .... <global> <events> <EventName> <observers> <moduleName_observer> <type>singleton</type> <class>PackageName_ModuleName_Model_Observer</class> <method>myActionMethod</method> </moduleName_observer> </observers> </EventName> </events> </global> ....
Obviously change EventName
and all fake name according to your package/module/methods names
The most of the difficult is to find the right event that suit you ...
Everytime you see in magento code something like Mage::dispatchEvent('EventName', Parameters);
this is an event.
you can find a list of default Magento event Here
I hope it helps you
这篇关于扩展/覆盖Magento中的扩展的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!