从管理页面重定向到前端的postAction


redirecting from admin page to a postAction on frontend

编辑的代码:现在我没有收到404错误,但另一方面,数据库或异常日志中没有添加任何内容。

我需要从我的模型php文件中调用第三部分模块上的postAction函数(来自ahead Works的VideoTestimonials),以便重用他们的所有代码,以获得所需的功能,并避免未来的错误,如果我只是复制代码并随意修改的话。

在之前的一个问题上,我想,通过使用setRedirect来实现这个目标的正确方向是正确的。以下是我在php模型文件中使用的代码,试图重定向到前端的postAction:

<?php
class Dts_Videotestimonials_Model_SearchVideo extends Mage_Core_Model_Abstract
{
    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', $data);
        Mage::app()->getResponse()->setRedirect($actionUrl);
    }
}

所有的修改都到了这里,好吧,一步一步(在@Francesco的帮助下)我到了这一步。但当我打这个电话时,我收到了一个404 Error。怎么了?

我在Magento论坛上查看了这个SO问题/答案和这篇帖子,但无法理清头绪。第二个是来自事件观察者的重定向,我不知道它是否有帮助。

以下是关于此错误的完整异常日志:

2012-09-04T14:25:17+00:00 ERR (3): 
exception 'Zend_Controller_Response_Exception' with message 'Invalid HTTP response code' in C:'wamp'www'magento'lib'Zend'Controller'Response'Abstract.php:286
Stack trace:
#0 C:'wamp'www'magento'lib'Zend'Controller'Response'Abstract.php(150): Zend_Controller_Response_Abstract->setHttpResponseCode(Array)
#1 C:'wamp'www'magento'app'code'core'Mage'Core'Controller'Response'Http.php(106): Zend_Controller_Response_Abstract->setRedirect('http://127.0.0....', Array)
#2 C:'wamp'www'magento'app'code'local'Dts'Videotestimonials'Model'SearchVideo.php(64): Mage_Core_Controller_Response_Http->setRedirect('http://127.0.0....', Array)
#3 C:'wamp'www'magento'app'code'local'Dts'Videotestimonials'Model'SearchVideo.php(198): Dts_Videotestimonials_Model_SearchVideo->printVideoEntry(Object(Zend_Gdata_YouTube_VideoEntry), Object(Mage_Catalog_Model_Product))
#4 C:'wamp'www'magento'app'code'local'Dts'Videotestimonials'Model'SearchVideo.php(244): Dts_Videotestimonials_Model_SearchVideo->printVideoFeed(Object(Zend_Gdata_YouTube_VideoFeed), Object(Mage_Catalog_Model_Product), 'Search results ...')
#5 C:'wamp'www'magento'app'code'local'Dts'Videotestimonials'controllers'Adminhtml'VideotestimonialsbackendController.php(28): Dts_Videotestimonials_Model_SearchVideo->searchAndPrint('s')
#6 C:'wamp'www'magento'app'code'core'Mage'Core'Controller'Varien'Action.php(419): Dts_Videotestimonials_Adminhtml_VideotestimonialsbackendController->postAction()
#7 C:'wamp'www'magento'app'code'core'Mage'Core'Controller'Varien'Router'Standard.php(250): Mage_Core_Controller_Varien_Action->dispatch('post')
#8 C:'wamp'www'magento'app'code'core'Mage'Core'Controller'Varien'Front.php(176): Mage_Core_Controller_Varien_Router_Standard->match(Object(Mage_Core_Controller_Request_Http))
#9 C:'wamp'www'magento'app'code'core'Mage'Core'Model'App.php(354): Mage_Core_Controller_Varien_Front->dispatch()
#10 C:'wamp'www'magento'app'Mage.php(683): Mage_Core_Model_App->run(Array)
#11 C:'wamp'www'magento'index.php(87): Mage::run('', 'store')
#12 {main}

如果你使用getUrl和getBaseUrl,这是一样的,你可以使用以下代码:

$this->_redirect('module/controller/action','parameters');

但是你已经在你的类上扩展了Mage_Adminhtml_Controller_Action。这是最好的方法。if不是一个控制器,所以你不应该做这个重定向。