phptypeerrorxenforo

TypeError related to XF\Pub\Controller\AbstractController


Disclaimer: I am new to XenForo 2.X programming, please go easy on me.

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

I can't figure out what I am doing wrong... Here's my current plan of attack, I have a very short snippet of code (a function included below), that I would like to be called in a XenForo Page using the PHP Callback. The criteria for the callback is using a controller and a reply reference as stated by xf:

  1. \XF\Pub\Controller\AbstractController $controller The controller instance. From this you can inspect the request, response etc.
  2. \XF\Mvc\Reply\AbstractReply &$reply The standard reply from the page controller.

the callback that is included in a node that's a Page is Pages\Providers::getData(). But running so gives me the TypeError. From my understanding, it is yelling at me that $controller the parameter 1 is not of correct type, but how? I don't even need it for my code. Any ideas on how to tackle this will be appreciated! Thank you.

<?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);
        }
    }
}

Solution

  • I have no idea what is xenforo. But your error message is clearly saying:

    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

    And in your code line 23 I think is this one:

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

    So just pass the first param not string but instance you have:

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