0

I want to add my database's data in layouts ,how to do it?thanks. I know the way such as $this->params['params'] = "params",but in function like beforeAction

  <div class="sBox">
            <div class="subNav sublist-down">
                <span class="title-icon glyphicon glyphicon-chevron-right"></span>
                <span class="sublist-title">资源管理</span>
            </div>
            <ul class="navContent" style="display:none">
                <li class="nav-li">
                    <div class="showtitle" style="width:100px;">
                        <img src="__ADMIN_IMAGES__/leftimg.png" />
                    </div>
                    <a href="<?= UrlService::buildWwwUrl('source/index'); ?>" target="right_content">
                        <span class="sublist-icon glyphicon glyphicon-record"></span>
                        <span class="sub-title">资源列表</span>
                    </a>
                </li>
            </ul>
        </div>
Josua Marcel C
  • 3,122
  • 6
  • 45
  • 87
pythonic
  • 7
  • 5
  • Possible duplicate of [How to horizontally center a
    in another
    ?](https://stackoverflow.com/questions/114543/how-to-horizontally-center-a-div-in-another-div)
    – Hemraj Pal Dec 21 '17 at 03:37
  • https://stackoverflow.com/questions/8438660/yii-how-to-retrieve-model-data-into-a-layout-page – Hemraj Pal Dec 21 '17 at 03:40
  • Possible duplicate of [Yii - how to retrieve model data into a layout page?](https://stackoverflow.com/questions/8438660/yii-how-to-retrieve-model-data-into-a-layout-page) – Yupik Dec 21 '17 at 07:11
  • I try this,But there is a error:exception 'ReflectionException' with message 'Method yii\web\View::widget() does not exist' when write '$this->widget('CategoryWidget')' in layouts main.php – pythonic Dec 21 '17 at 07:14

1 Answers1

0

The question isn't quite clear. Show your layout and data which you want to use.

Here are many different ways to use data in layout:

  • Using widgets. You can create own widget, it's simple
  • Using application components or application itself. For example \Yii::$app->params (you mentioned this in question)
  • Using $this variable (\yii\web\View), for example it's common practice to use $this->title which can be assigned in controller
  • Using View data. Layout have access to all params passed to view. This can be acceptable if you have some data format convention for all controllers

Which of them is the right one for you depens on the problem you solve.

oakymax
  • 1,454
  • 1
  • 14
  • 21
  • thanks your answer.Itry to use widgets.,But there is a error:exception 'ReflectionException' with message 'Method yii\web\View::widget() does not exist' when write '$this->widget('CategoryWidget')' in layouts main.php.how to solve ? – pythonic Dec 21 '17 at 07:18
  • @pythonic if `CategoryWidget` is the a widget class then you should use it as `= CategoryWidget::widget() ?>`. Take a look at docs http://www.yiiframework.com/doc-2.0/guide-structure-widgets.html – oakymax Dec 21 '17 at 07:31
  • @oakymax Not sure, if widgets solved this problem, because if a widget needs model data so the data is still not available in layout during rendering. So you have to set public parameter like `$this->model = $model` Also, your last point: "Layout have access to all params passed to view." should not work. YII2 docs remomend the way using `$this->params['...` https://www.yiiframework.com/doc/guide/2.0/en/structure-views#sharing-data-among-views – The Bndr Nov 16 '21 at 13:16