如何获取yii2注册资产列表来创建SPF.js响应数组?

时间:2015-11-21 14:58:09

标签: php arrays json yii2 yii2-basic-app

我需要获取在页面中注册的yii2资产列表,以便为响应SPF.js请求创建数组 我在名为ajax.php的新布局中使用此代码

<?php
use yii\helpers\Html;
use app\assets\AppAsset;
use \yii\helpers\Json;

AppAsset::register($this);
$spf_data = [
    'title' => Html::encode($this->title),
    'head' => $this->beginPage() . Html::csrfMetaTags() . $this->head(),
    'body' => ['content' => $this->beginBody() . $content],
    "attr" => [
        'content' => [
            "class" => "container"
        ]
    ],
    'foot' => $this->endBody() . $this->endPage(),
];
echo Json::htmlEncode($spf_data);

我的问题是注册资产到页面,我无法在我的阵列中获取它们
我需要在数组的头索引中获取元标记列表,链接标记和脚本
我怎么办?

对不起英语不好

1 个答案:

答案 0 :(得分:0)

您可以使用此代码

在控制器操作中获取脚本和样式
ob_start();
ob_implicit_flush(false);
$this->view->beginPage();
$this->view->head();
$this->view->beginBody();
$this->renderPartial($view, $params);
$this->view->endBody();
$this->view->endPage(true);
$style_script= ob_get_clean();

并在$style_script

中使用$spf_data
$spf_data = [
    'title' => Html::encode($this->title),
    'head' => $style_script,
    'body' => ['content' => $this->renderPartial('viewName')],
    "attr" => [
        'content' => [
            "class" => "container"
        ]
    ],
];

此代码适用于我