Pass title from controller to layout or view

In a controller I have declared

public $title = "";

And here is the method:

    public function actionView()


        $dogid = Yii::$app->request->get('dogid');

        $quy = Yii::$app->db->createCommand('SELECT * FROM dc_dogs WHERE dogid=:dogid')

                ->bindValue(':dogid', $dogid)


        $title = 'dog view';

        $this->layout = 'dog/viewtp';

        return $this->render('view', compact('quy', $quy, 'title', $title));


And in the layout at top I have:

<title><?= $this->title; ?></title>

Everything is working, but the title. Instead of a title I want, the actual title being displayed is the entire url.

I am getting


for the title instead of

dog view

OK how to I pass the data and $title. The $quy is passing over just fine.

By the way I did try

<title><?= $title; ?></title>

that throws an error.


Please explain this, if I add to top of layout


use yii\helpers\Html;


top of view


use yii\helpers\Html;

$this->title = 'Dog view';


And in layout in head

<title><?= Html::encode($this->title) ?></title>

It works.

My question is when I render above, $quy is available in the layout, because you do

<?= $content; ?>

In the layout. I even tried $mytitle, still did not work. Seems variables passed in a compact would be there in the layout as well.

Kind of backwards assigning a title in a view to echo it in the layout. The layout is where the head section sits.

Please explain on this.

Bottom line, how would you pass not only the data, but also other parametere / variables to the layout. Of course the data is passed along in $content.

In laravel I can build up parameters to pass to view and layout

        $dogs = DB::table('dc_dogs')

                        ->where('adopted', '=', 0)


        $pagelinks = LengthPager::makeLengthAware($dogs, $numrows, $perpage, ['t1' => $t1]);

        $title = 'Dogs';

        $view = 'dog/index';

        $layout = ViewLayout::getLayout('dog/indextp');

        $header = View::make(ViewLayout::getLayout('headertp'));

        $content = View::make($view)

                ->with('dogs', $dogs)

                ->with('pagelinks', $pagelinks);

        return view($layout)->with('content', $content)->with('header', $header)->with('title', $title);

Notice the ->with(‘title’, $title)

Sometimes I need to pass a bunch of things to layout and view.

In the above laravel example I am passing a whole section to the layout file that used in several layouts:

You can build up several with


I don’t see this in Yii, so big ? there has got to be a way to pass some things to the layout.

The key line is of course

return view($layout)->with('content', $content)->with('header', $header)->with('title', $title);

Notice the return view is the layout, but it puts things where they belong, title in the layout, dogs and pagelinks in the view, etc.

Yii has to be able to do this stuff.

Well I found this, in controller

$this->view->params['mytitle'] = 'doggie';   //Just example

Then Layout

<title><?= $this->params['mytitle']; ?></title>


The way Yii shows to to it by setting it in view, then using in layout is like WHAT!

So much more direct having it right in the controller method and use in the layout.

Still need help, I also need to pass data to the view, but also at same time pass other header stuff to the layout, all from controller. It is easy in laravel, but there has to be a way in yii.

Basically in the layout I have several links that are the same on numerous pages. I normally have them in a headertp.php file. So when I do this in controller:

return $this->render('view', compact('quy', $quy));

But I need something like this:

Whatever code to make a view here (for headertp)


return $this->render('view', compact('quy', $quy))->with(yii doesn't have);

// kinda like

        $view = 'dog/index';

        $layout = 'template/dog/indextp';

        $header = View::make('template/headertp');

        $content = View::make($view)

                ->with('dogs', $dogs)

                ->with('pagelinks', $pagelinks);

        return view($layout)->with('content', $content)->with('header', $header);

I guess if yii2 can’t render more than one thing (like make::view) I can always go old school

<?php require VIEWPATH . 'header' . '.php'; ?>  // something like that

Just in laravel you can view::make multi times, and pass all that from controller.

All I am after is like this example laravel layout I use:

<!DOCTYPE html>

<html lang="en">


        <title><?= $title; ?></title>

        <link href="<?php echo asset('assets/css/dog/style.css'); ?>" rel="stylesheet">

        <script type="text/javascript" src="<?php echo asset('assets/js/jquery.js'); ?>"></script>



        <?php echo $header; ?>  --->>> here just links that are on several pages

        <div class="cf"></div>

        <div class="dgname">

            Vaccinated and spayed or neutered before being adopted


        <?php echo $content; ?>



It’s the <?php echo $header; ?> part I can’t figure out how to build it in the controller method and pass the information to the layout.

Found in docs renderPartial(): renders a named view without any layout.

And the API doc shows See here

But no good examples, require is looking better and better.


Finally dug this out:

In layout I

<?php echo \Yii::$app->view->renderFile(Yii::getAlias('@app') . '/views/layouts/headertp.php'); ?>

It took a while, the API shows

public string renderFile ( $file, $params = [] )

The API does not tell you to add \Yii::$app->view-> WHY?

I am just used of building this stuff in controller first.