方法一. 重写 yii\web\Controller::afterAction($action, $result) 方法:namespace frontend\controllers;
use Yii;
use yii\helpers\FileHelper;
use yii\helpers\Url;
use yii\web\Controller;
class ArticleController extends Controller
{
public function afterAction($action, $result)
{
$route = $action->controller->actionParams;
array_unshift($route, $action->controller->id . '/' . $action->id);
$path = Url::to($route); // '/article/view/104.html'
if(substr($path, 0, 1) === '/'){
$path = substr($path, 1); // 'article/view/104.html'
}
FileHelper::createDirectory(dirname($path)); // 创建目录
file_put_contents($path, $result); // 写入文件
return parent::afterAction($action, $result); // TODO: Change the autogenerated stub
}