Yii2-独立动作如何遵循定义的布局

时间:2019-04-30 15:01:14

标签: php yii2

独立操作如何遵循已定义的布局? 内联动作非常简单,其中布局是从@ views / layout获取的。

布局

<?php

use yii\helpers\Html;
use yii\helpers\Url;

/* @var $this \yii\web\View */
/* @var $content string */

$favicon = Url::to('@web/img/logo.png');

app\assets\AppAsset::register($this);
app\assets\AdminLteAsset::register($this);
app\assets\AdminLtePluginAsset::register($this);
app\assets\BackendAsset::register($this);
$directoryAsset = Yii::$app->assetManager->getPublishedUrl('@vendor/almasaeed2010/adminlte/dist');
?>

<?php $this->beginPage() ?>
<!DOCTYPE html>
<html lang="<?= Yii::$app->language ?>">
<head>
    <meta http-equiv="content-type" content="text/html;  charset="<?= Yii::$app->charset ?>"/>
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <?= Html::csrfMetaTags() ?>

    <?php $this->registerLinkTag(['rel' => 'icon', 'type' => 'image/png', 'href' => "$favicon"]); ?>
    <title><?= Html::encode($this->title) ?></title>

    <?php $this->head() ?>

</head>
<body class="hold-transition <?= \dmstr\helpers\AdminLteHelper::skinClass() ?> sidebar-mini fixed">
<div class="se-pre-con"></div>
<?php $this->beginBody() ?>
<div class="wrapper">

    <?=
    $this->render(
        'header.php', ['directoryAsset' => $directoryAsset]
    )
    ?>

    <?=
    $this->render(
        'left.php', ['directoryAsset' => $directoryAsset]
    )
    ?>

    <?=
    $this->render(
        'content.php', ['content' => $content, 'directoryAsset' => $directoryAsset]
    )
    ?>

</div>

<?php $this->endBody() ?>
</body>
</html>
<?php $this->endPage() ?>

CONTROLLER

class BillOfLadingController extends Controller{

    public function actionIndex(){
        $searchModel = new BillOfLadingSearch();
        $dataProvider = $searchModel->search(Yii::$app->request->queryParams);

        return $this->render('index', [
            'searchModel' => $searchModel,
            'dataProvider' => $dataProvider,
        ]);
    }
}

转换为独立操作

class BillOfLadingController extends Controller{

    public function actions(){
        $searchModel = new BillOfLadingSearch();
        $dataProvider = $searchModel->search(Yii::$app->request->queryParams);

        return [
            'index' => [
                'class' => 'app\common\actions\admin\billOfLading\indexAction',
                'searchModel' => $searchModel,
                'dataProvider' => $dataProvider,
                'layout' => $this->layout
            ],
        ];
    }
}

然后在app\common\actions\admin\billOfLading\indexAction类中, 我使用小部件作为视图

将上面的控制器代码更改为使用独立操作时,

<?php
namespace app\common\actions\admin\billOfLading;

use app\common\widgets\admin\billOfLading\BillOfLadingWidget;
use yii\base\Action;

class indexAction extends Action
{
    public $searchModel;
    public $dataProvider;

    public function run()
    {
        try {
            return
                BillOfLadingWidget::widget([
                    'searchModel' => (object)$this->searchModel,
                    'dataProvider' => (object)$this->dataProvider,
                ]);
        } catch (\Exception $e) {
            return $e->getMessage();
        }
    }

}

小部件

<?php

namespace app\common\widgets\admin\billOfLading;

use yii\base\Widget;

class BillOfLadingWidget extends Widget
{

    public $searchModel;
    public $dataProvider;

    public function init()
    {
        parent::init();
    }


    public function run()
    {
        return $this->render('index', [
            'dataProvider' => $this->dataProvider,
            'searchModel' => $this->searchModel
        ]);
    }

    public function getViewPath()
    {
        return '@app/common/widgets/admin/billOfLading/views/';
    }
}

布局不起作用。 只有小部件的显示。

1 个答案:

答案 0 :(得分:1)

好吧,我不确定是什么要求迫使您扩展yii\base\Action类而不是扩展yii\web\ViewAction类。

但是,如果仍然要这样做,则可能需要在$layout内声明属性indexAction,因为$layout不是{{1 }}类,您都没有在自定义yii\base\Action中定义。

然后,您必须像在yii\web\ViewAction类中那样添加布局代码

IndexAction

现在,您的“独立”操作将应用布局。