Yii 2 Test Controller

Ok, so let’s look at the TestController.php:


<?php

namespace frontend\controllers;

class TestController extends \yii\web\Controller
{
    public function actionIndex()
    {
        return $this->render('index');
    }

}

That’s it. You can see it consists of very little except the actionIndex method, and all that is doing is rendering a view named index. Let’s look at the view:


<?php
/* @var $this yii\web\View */
?>
<h1>test/index</h1>

<p>
    You may change the content of this page by modifying
    the file <code><?= __FILE__; ?></code>.
</p>

Again, almost nothing here, so very easy to play with. Try yourdomain/index.php?r=test and you should see the view, assuming you have Yii 2 setup correctly. Let me just mention that is not a starting point for learning Yii 2. If you have not setup your application, consult the official guide or, if you want help oriented towards beginners, follow the step by step instructions in my book.

Ok, so back to the test controller and view. Think of it as a blank canvass that you can use to try different snippets of code or to figure out how something works or what is contained in var_dump().

Advertisements

One thought on “Yii 2 Test Controller”

  1. Thank you so much for this.
    I have been attempting to learn Yii 2.0 for about 2 weeks now and stumbling and fumbling my way through.
    A few minutes into this tutorial and it clicked in.
    You’ve done a great job of easily explaining how it all works, so thank you for taking the time to put this down for everyone to learn from.

    Cheers,
    -Colin

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s