Search code examples
phparraysjsonyii2yii2-basic-app

How to get yii2 registered assets list for creating SPF.js response array?


I need getting list of yii2 assets registered in the page for creating an array for response SPF.js requests I use this code in a new layout named 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);

my problem is registered assets to page and i can't get them in my array
i need to get list of meta tags,link tags and script in head index of array
how can I do?


Solution

  • You can get scripts and styles in your controller action with this code

    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();
    

    and use $style_script in $spf_data

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

    this code work for me