XF 2.1 PHP callbacks are not working

researcher

Member
Hey!
I am trying to insert my own PHP codes on a custom page. I am adding the callbacks but it never works... Its what I did:

1) Created a directory as named TestCallbacks and put an index.php inside this directory.
(Full path: /var/www/vhosts/example.com/httpdocs/library/TestCallbacks/index.php)

2) index.php codes:
PHP:
<?php
class TestCallbacks_index {
public static function getHtml(){
echo 'hello';
// tried return 'hello'; too and not worked
}
}

3) When I am trying to write the class name as TestCallbacks_index:getHtml I got error:

111111111111111.webp

Also, I have tried <xen:callback class="TestCallbacks" method="getHtml"></xen:callback> code but it did not worked too, I got empty page or error_invalid_class error.

My XF version is: 2.1.4 , how can I solve this problem?

Thanks.
 
The signature is not valid.

callback-signature.png

PHP:
<?php
class TestCallbacks_index
    {
        public static function getHtml(\XF\Pub\Controller\AbstractController $controller, \XF\Mvc\Reply\AbstractReply &$reply)
        {
            // echo won't work
            // echo 'hello';
            // tried return 'hello'; too and not worked
        }
    }
}
 
The signature is not valid.

View attachment 214887

PHP:
<?php
class TestCallbacks_index
    {
        public static function getHtml(\XF\Pub\Controller\AbstractController $controller, \XF\Mvc\Reply\AbstractReply &$reply)
        {
            // echo won't work
            // echo 'hello';
            // tried return 'hello'; too and not worked
        }
    }
}

Updated index.php as:
PHP:
<?php
class TestCallbacks_index
    {
        public static function getHtml(\XF\Pub\Controller\AbstractController $controller, \XF\Mvc\Reply\AbstractReply &$reply)
        {
           return 'tesssttt';
        }
    }

but still the same problem... Callback TestCallbacks_index::getHtml is invalid (error_invalid_class).
 
Returning a string won't work and you really want to read the documentation, but anyway:
You did save this file as src/addons/TestCallbacks_index.php?
Yes! Finally system detected my class, very thanks... But a little question: how can I print a text? echo 'hello'; and return 'hello'; didnt worked, what can I do?
 
But a little question: how can I print a text?
Short answer: You can't.
Long answer: You can manipulate the reply, read the devloper documentation to understand how XF2 does work.

Really, really, really bad "Hello World" type example:
PHP:
<?php
class TestCallbacks_index
{
    public static function getHtml(\XF\Pub\Controller\AbstractController $controller, \XF\Mvc\Reply\AbstractReply &$reply)
    {
       $reply->setParam('foobar', 'fooBAR');
    }
}

And use {$foobar} in the HTML of the page.

But as said, you should do this properly, eg. create an Add-on and place the appropriately named class where it belongs.
 
Last edited:
Thank you very much! :)

Short answer: You can't.
Long answer: You can manipulate the reply, read the devloper documentation to understand how XF2 does work.

Really, really, really bad "Hello World" type example:
PHP:
<?php
class TestCallbacks_index
{
    public static function getHtml(\XF\Pub\Controller\AbstractController $controller, \XF\Mvc\Reply\AbstractReply &$reply)
    {
       $reply->setParam('foobar', 'fooBAR');
    }
}

And use {$foobar} in the HTML of the page.

But as said, you should do this properly, eg. create an Add-on and place the appropriately named class where it belongs.
 
Top Bottom