When a file_get_contents($url)
is used inside a action and when that action is loaded using Pjax, the entire page reloads.
In controllers/SiteController.php
public function actionAbout()
{
$url = 'http://api.dar.fm/topsongs.php?q=Music&page_size=20';
$xml = file_get_contents($url);
Yii::$app->view->params['xmldata'] = $xml;
return $this->render('about');
}
In layouts/main.php
<?php Pjax::begin(); ?>
<a href="/yiidev/web/index.php?r=site/home">Home</a>
<a href="/yiidev/web/index.php?r=site/about">About</a>
<a href="/yiidev/web/index.php?r=site/contact">Contact us</a>
<?php Pjax::end(); ?>
For Home and Contact link, only the area between pjax begin()
and end()
is updated but for About link the entire page reloads.
If i remove the file_get_contents()
call from from actionAbout()
the page reload is not happening. I believe the problem is something related to getting contents from external url using file_get_contents()
The issue was due to ajax timeout. file_get_contents() took more time to execute, as it was reading a external url and timeout happens. The issue was resolved by increasing timeout vaue as below.
Pjax::begin(['timeout' => 5000 ]);
Refer https://github.com/yiisoft/yii2/issues/8819