XF 2.1 Custom PHP Page

Well, I want PHP & HTML mixed ^_^

would be nice, if you could tell me how it works.

I tried to create a folder in ../src/addons/MyOwnPage/index.php

And created a template with the following code:

Code:
<xf:callback class="\MyOwnPage\Index" method="getHtml"></xf:callback>

My index.php looks like:

Code:
<?php
namespace MyOwnPage;
class Index
{
  public static function getHtml()
  {
    //return 'Test html';
    echo "testing...";
  }
}
 
He probably meant to use the node "Pages".

ACP->Forums->Nodes->Add Node->Page
 
What exactly do I need to do then? :) is there any step by step tutorial?
I don't think in the manual there is one.
However you could take a look at this for XF1 (which is the same thing more or less).
 
I was actually able to get this working, here's my code:

PHP:
<?php

/*
* This file is part of a XenForo add-on.
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Pages;

/**
* Static methods for the Providers page.
*/
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
    ) {
        $active = 1;
        if ($reply instanceof \XF\Mvc\Reply\View) {
                $finder = \XF::finder('XF:Thread');
                $thread = $finder->where('thread_id', $active)->fetchOne();
                $firstPost = \XF::app()->finder('XF:Post')->where('post_id', $thread['first_post_id'])->fetchOne();
                $viewParams = [
                    'title' => $thread['title'],
                    'message' => $firstPost['message']
                ];
                $reply->setParams($viewParams);
        }
    }
}

And in the nodes, I created a page and using the php callback provided within the page node I have: Pages\Providers as my class and getData as my method.

Now that you have all that set up, you can simply change $active to whatever thread you would like to display within the page. You can use variables such as {$message} within the body of your page to actually display the message (or w/e it may be).
 
Now, what I am stuck on is how to send a variable to getData function to easily switch between what I wanna display, instead of having a static post number (in the above case, it is 1).
 
I was actually able to get this working, here's my code:

PHP:
<?php

/*
* This file is part of a XenForo add-on.
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Pages;

/**
* Static methods for the Providers page.
*/
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
    ) {
        $active = 1;
        if ($reply instanceof \XF\Mvc\Reply\View) {
                $finder = \XF::finder('XF:Thread');
                $thread = $finder->where('thread_id', $active)->fetchOne();
                $firstPost = \XF::app()->finder('XF:Post')->where('post_id', $thread['first_post_id'])->fetchOne();
                $viewParams = [
                    'title' => $thread['title'],
                    'message' => $firstPost['message']
                ];
                $reply->setParams($viewParams);
        }
    }
}

And in the nodes, I created a page and using the php callback provided within the page node I have: Pages\Providers as my class and getData as my method.

Now that you have all that set up, you can simply change $active to whatever thread you would like to display within the page. You can use variables such as {$message} within the body of your page to actually display the message (or w/e it may be).

Hi, thanks for sharing! Which path do I need exactly for the .php file on the webserver?
 
Hi, thanks for sharing! Which path do I need exactly for the .php file on the webserver?

I put the php file in src/addons/Pages/Providers.php.

Btw, I am still learning and am very new, this may not be the best way to set up things. My goal was to display specific posts in the page format of xF.
 
I put the php file in src/addons/Pages/Providers.php.

Btw, I am still learning and am very new, this may not be the best way to set up things. My goal was to display specific posts in the page format of xF.

1levx.png
 
Doesnt work for me :/ and doesn't really fit what I need aswell, but nice that you share your results!

Hope someone can help me here ^_^
Reading back -- what you were trying should work. Just call the following command INSIDE your Page (ACP->Forums->Nodes->Add Node->Page)

Code:
<xf:callback class="\MyOwnPage\Index" method="getHtml"></xf:callback>

Whatever you return from your php function should display in the page.
 
Reading back -- what you were trying should work. Just call the following command INSIDE your Page (ACP->Forums->Nodes->Add Node->Page)

Code:
<xf:callback class="\MyOwnPage\Index" method="getHtml"></xf:callback>

Whatever you return from your php function should display in the page.

The problem is that my PHP File is mixed with HTML, and data is submitted via POST submit. I don't really know how I can do that with XenForo. In XF1 there was an addon that I could do custom pages easily on my own, without classes etc.
 
So far so good, it works... BUT now I get "security error occured", when I hit the submit-button. Any idea how I can fix that?

pnuws.png
 
I put the php file in src/addons/Pages/Providers.php.

Btw, I am still learning and am very new, this may not be the best way to set up things. My goal was to display specific posts in the page format of xF.

Can you post a screenshot of how that looks?
 
Top Bottom