与XF \ Pub \ Controller \ AbstractController相关的TypeError

时间:2019-04-25 02:08:52

标签: php typeerror xenforo

免责声明:我是XenForo 2.X编程的新手,请放心。

An exception occurred: [TypeError] Argument 1 passed to 
Pages\Providers::getData() must be an instance of 
XF\Pub\Controller\AbstractController, string given in 
src/addons/Pages/Providers.php on line 23

Pages\Providers::getData()
call_user_func() in src/XF/Template/Templater.php at line 1382
XF\Template\Templater->callback() in internal_data/code_cache/templates/l1/s2/public/_page_node.100.php at line 13
XF\Template\Templater->{closure}() in src/XF/Template/Templater.php at line 1294
XF\Template\Templater->renderTemplate() in src/XF/Template/Templater.php at line 1367
XF\Template\Templater->includeTemplate() in internal_data/code_cache/templates/l1/s2/public/page_view.php at line 82
XF\Template\Templater->{closure}() in src/XF/Template/Templater.php at line 1294
XF\Template\Templater->renderTemplate() in src/XF/Template/Template.php at line 24
XF\Template\Template->render() in src/XF/Mvc/Renderer/Html.php at line 48
XF\Mvc\Renderer\Html->renderView() in src/XF/Mvc/Dispatcher.php at line 418
XF\Mvc\Dispatcher->renderView() in src/XF/Mvc/Dispatcher.php at line 400
XF\Mvc\Dispatcher->renderReply() in src/XF/Mvc/Dispatcher.php at line 360
XF\Mvc\Dispatcher->render() in src/XF/Mvc/Dispatcher.php at line 53
XF\Mvc\Dispatcher->run() in src/XF/App.php at line 2177
XF\App->run() in src/XF.php at line 390
XF::runApp() in index.php at line 20

我不知道自己在做什么错...这是我目前的攻击计划,我有很短的代码片段(下面包含一个函数),我想在XenForo页面中调用它使用PHP回调。回调的条件是使用控制器和xf所述的回复引用:

  1. \XF\Pub\Controller\AbstractController $controller控制器 实例。由此您可以检查请求,响应等。
  2. \XF\Mvc\Reply\AbstractReply &$reply页面的标准回复 控制器。

页面节点中包含的回调为Pages\Providers::getData()。但是这样运行给我TypeError。据我了解,$controller参数1不是正确类型,但又如何呢?我的代码甚至不需要它。关于如何解决这个问题的任何想法将不胜感激!谢谢。

<?php

namespace Pages;

class Providers
{
    /**
     * @param \XF\Pub\Controller\AbstractController $controller
     * @param \XF\Mvc\Reply\AbstractReply           &$reply
     */
    public static function getData(
        \XF\Pub\Controller\AbstractController $controller,
        \XF\Mvc\Reply\AbstractReply &$reply
    ) {
        if ($reply instanceof \XF\Mvc\Reply\View) {
                $finder = \XF::finder('XF:Thread');
                $thread = $finder->where('thread_id', 1)->fetchOne();
                $firstPost = \XF::app()->finder('XF:Post')->where('post_id', $thread['first_post_id'])->fetchOne();
                $viewParams = [
                    'title' => $thread['title'],
                    'message' => $firstPost['message']
                ];

                // return $firstPost['message'];
                $reply->setParam('providers', $viewParams);
        }
    }
}

1 个答案:

答案 0 :(得分:1)

我不知道什么是xenforo。但是您的错误消息清楚地表明:

  

发生异常:[TypeError]参数1传递给   Pages \ Providers :: getData()必须是   XF \ Pub \ Controller \ AbstractController,字符串在   src / addons / Pages / Providers.php,第23行

在您的代码行23中,我认为是这样的:

$reply->setParam('providers', $viewParams);

因此,只需传递第一个参数(不是字符串,而是实例),就可以了:

$reply->setParam($controller, $viewParams);