XF 2.2 Integrate XenForo footer and header for stand alone .php file

JohnLogar

Active member
I'm trying for few days to integrate the footer and header of XenForo on a standalone PHP file but stuck.

The same thing was done before here for XenForo 1

In file separate.php
Code:
$container = true;
$fileDir = dirname(__FILE__)."/{$indexFilePath}";
$dir = __DIR__;
require($dir . '/src/XF.php');

\XF::start($dir);

require "{$fileDir}src/AddonPath/Header.php";

Header.php
Code:
$objFC = new FrontController(\XF::app(),\XF::app()->request());

FrontCroller Class
Code:
class FrontController extends \XF\Mvc\Dispatcher
{
    public function runDriven()
    {
   
       $html="
       <html>
       <head>
       <title>Some Title</title>
       </head>
       <body>
       Hello world!
       </body>
       </html>";
      
       return new \XF\Mvc\Reply\View(
        '',
        'sep_template',
        ['html'=>$html]
    );
    
    }

At the separate.php footer i called the run() function with blank screen output.

Your help would be appericiate.

Thanks
 

JohnLogar

Active member
Thanks, @Lukas W. for your reply.

If i try to launch the app
Code:
\XF::runApp('XF\Pub\App');
in seperate.php. It will redirect the separate.php file to the home index page. In runApp() function it checks if the route not matched because we accessing without route and it redirects to index.php.

After more searching, I checked there is class Dispatcher with the renderView function
Code:
    public function renderView(AbstractRenderer $renderer, Reply\View $reply)
    {
        $params = $reply->getParams();
        $template = $reply->getTemplateName();
        if ($template && !strpos($template, ':'))
        {
            $template = $this->app['app.defaultType'] . ':' . $template;
        }
    
        return $renderer->renderView($reply->getViewClass(), $template, $params);
    }

I think, there is magic I need to do. I have created an object of the class in separate.php and provided the $render parameter and need to provide the View class object as the second parameter $reply and I need to get help how I can get the View Class object here to pass.

Code:
$dispatcher=new Dispatcher(\XF::app(),\XF::app()->request());
$renderer = \XF::app()->renderer('html');
$reply="need view class object here';
return $dispatcher->renderView($renderer,$reply);

I'm sorry for the broken English but if you did not understand anywhere I'm happy to explain.

Thanks
 

Lukas W.

Well-known member
You don't need to initiate the entire dispatch process. Starting the app (XF::start($dir);) initiates all basic processes and makes the renderer and other needed objects available.
 

JohnLogar

Active member
I already did It. but not helpful. here is the code

Code:
$dir = __DIR__;
require($dir . '/src/XF.php');

\XF::start($dir);
$dispatcher=new Dispatcher(\XF::app(),\XF::app()->request());
$renderer = \XF::app()->renderer('html');
$replyView=new View('XF:Forum\Listing','public:forum_list',['html'=>'testing']);
$replyView->setResponseCode(200);
$replyView->setAction('List');
$replyView->setControllerClass('XF\Pub\Controller\Forum');
$replyView->setResponseType('html');
$replyView->setSectionContext('forums');
$renderer->setReply($replyView);
$dispatcher->renderView($renderer,$replyView);

It returns the blank screen.
 

Lukas W.

Well-known member
That's absolute overkill and not the right process. You need to initiate the templater with global objects and then render the PAGE_CONTAINER template with the right parameters. You don't need to construct a view or run the dispatcher.
 

JohnLogar

Active member
Hi @Lukas W.
I removed all other processes and just use two of the following in the test.php file

Code:
$dir = __DIR__;
require($dir . '/src/XF.php');
\XF::start($dir);
\XF::runApp('XF\Pub\App');

I have created a template with a global object but in this function, once i access the localhost/xf2/test.php the reply parameter of the global templater return as Redirect Object which returns by
Code:
\XF::runApp('XF\Pub\App');

But I need it by View Object, So, I can set the template by $reply->setView('my_temp'). That's the issue that not allows me to move forward. I want to use my own template rather than Page_Container. I hope you will guide me more on it.

Thanks
 
Top