XF 2.0 Easiest way to call a php script

indepth

Member
I need to call a very simple php script to return a user value and display it in the nav bar.

What is the easiest (correct) way to do this?

Can you make the call inside a style with the additional html? Or does it need to be inside a template? Or are there other restrictions?
 
You'll never be able to do that with a style or html :unsure:
Pinpoint the nav bar template, then create a callback tag, then create your callback class script. This would be one and most likely the easiest way.
The proper way *I believe* to do this is to extend the controller / view, add your additional stuff to viewParams and hook that in. This way, you could use your variables in the template.
 
Thanks for the reply @yoloswaggerino

Surely there's got to be a reasonably easy way. All I'm trying to do is add a numeric character at the top of the nav bar.

If I go the route of creating a callback tag and then a callback class script; where does that go?
 
I found someone who posted an example like this:

Code:
<?php

  namespace MyFirstCallback;

  class genericClassName {

    public static function getthis() {

      return 'Hello world!';

    }

  }

?>

<xf:callback class="MyFirstCallback\genericClassName" method="getthis"></xf:callback>

But I'm not sure where in the directory structure to place the PHP class? Or does it need to go into an existing XF file?
 
That worked! Thank you.

Now I've realized that it appears the compile order prohibits use of the session variables in my callback script.

What would the command be to pass that function something to identify the user such as the XF_usernumber?
 
I'm not sure if this is the correct way or not, but I was able to get this to work as follows.

Code:
<?php
  namespace MyCallbacks;

  class myClass {

    public static function getthis() {
        
        $arguments = func_get_arg(1);
      
        $xfuserid = $arguments[0];
        
      return "<li><a href='#'>Your UserID is: $xfuserid</a></li>";
    }

  }

?>

In the file /src/addons/MyCallbacks/myClass .php

and the call in the template:
<xf:callback class="MyCallbacks\myClass" method="getthis" params="[{$xf.visitor.user_id}]"></xf:callback>
 
params="[{$xf.visitor.user_id}]" => you can call that explicitly with $params[0]. You will need to adjust the function header, though.
 
params="[{$xf.visitor.user_id}]" => you can call that explicitly with $params[0]. You will need to adjust the function header, though.

I tried adding $params as a function variable and calling it explicitly, but I must have had the syntax wrong because it threw an error.
 
Actually, it threw an error when I tried to call the function as getthis('16);

It simply displayed an empty array when I tried to var_dump $params;
 
Did you change the function header? Shoulda look like
Code:
public static function getthis($content, $params) {
this. These 2 are available to all callback functions. At least they used to be :unsure:
 
Top Bottom