one page template in yii2


Just started to play around with yii 2, there is one area I am not sure about.

I have a website in html that has only a single page with different sections(those new templates where you slide up and down when you click the top nav buttons).

I am confused on how to integrate it into Yii2.

For example in this file:


There is this code:

    } else {

        $menuItems[] = [

            'label' => 'Logout (' . Yii::$app->user->identity->username . ')',

            'url' => ['/site/logout'],

            'linkOptions' => ['data-method' => 'post']



    echo Nav::widget([

        'options' => ['class' => 'navbar-nav navbar-right'],

        'items' => $menuItems,




    <div class="container">

        <?= Breadcrumbs::widget([

            'links' => isset($this->params['breadcrumbs']) ? $this->params['breadcrumbs'] : [],

        ]) ?>

        <?= Alert::widget() ?>

        <?= $content ?>



<footer class="footer">

    <div class="container">

        <p class="pull-left">&copy; My Company <?= date('Y') ?></p>

        <p class="pull-right"><?= Yii::powered() ?></p>



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



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

If let’s say you have a website cut into sections like this(on 1 single page)

=========section one=============



=========section two=============



=========section three=============



=========section four=============



How would you integrate this into the layout.php file please?

Thank you,

Just put that in the views/site/index.php page.

The layout is what is around those 4 sections.

If you insist on keeping it modular, then you can later break that views/site/index.php file up, like this:

<?= $this->render('partials/index/_one'); ?>

<?= $this->render('partials/index/_two'); ?>

<?= $this->render('partials/index/_three'); ?>

<?= $this->render('partials/index/_four'); ?>

That requires a folder called ‘partials’ with four files in it.

Like the first file: views/site/index/partials/_one.php …

But, for now, just edit or create views/site/index.php with your 4 sections.

The layout will (or should) render head, top and bottom of your page.

And your view is everything in $content.

Thanks I did not see your s post fast enough.

What I did was put the whole one page template in the layout/main.php, created a few models and was hoping to fill up the needed data with a few queries, would this be fine? I have also removed the "$content"

I have divided each section into model and would like to make 1 crud per model which will work out the same.

But I am not sure if this is standard to yii coding.


That is definitely not standard Yii coding :P

No, you need to treat the main layout - which the controllers uses by default - as a layout.

$content is where your view files will ‘end up’.

Read the guide :)

-> http://www.yiiframew…ture-views.html

If you want to break the rules, first you need to understand the rules.

Keep note of the difference between layout and view - it is important.

Thank you, I will check it out. Thanks again.

On a side note, I wrote another post on this), is it normal that whatever ever path you set in the GII generator for the views, all the views get generated in the frontend/web folder.

I am really struggling with this one…I tried to even leave the "view path" input filed empty…no chance, everything I generate view wise goes in that web folder but all my controllers and models are going in the correct backend models and controllers folders.

Di you even experience this please?


Probably best to keep the discussion of that other issue in the dedicated post ;)

Yes you are right, thanks Jacmoe:-)

jacmoe, I tried to put everything in the index.php, it worked, everything shows but there is a problem, my original template was using the whole width of the page, now by putting the template in the index.php, the layout is squashed into a defined width frame, any idea how to get around that with Yii2 please?

Thank you,

You have to be more specific… ;)

Do you mean that you used ‘container-fluid’ ?

If so, you need to set ‘container’ to ‘container-fluid’ in the main layout (views/layouts).

And, if you have any additional ‘container’ tags, they need to have -fluid as well.