Yii 2 Test Controller

So in TestController.php, I changed the actionIndex method to the following:

ublic function actionIndex()
          $date = "Sep 6, 2014";
         $date = Yii::$app->formatter->asDate($date, "Y-M-d");
         return $this->render('index', [
            'date' => $date

You can see in the return statement that we tell it which view to render as well as sending along objects or variables, in this case, we are sending $date.

Then in my index.php view:

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

     <?php echo 'The date is '. $date ;?>.

So the first thing that happens when I try the page is I get the following error:

PHP Fatal Error – yii\base\ErrorException

Class ‘frontend\controllers\Yii’ not found

This is because we are calling the formatter via the yii container class, but I did not include a use statement for it in my test controller. So add this to the controller:

use yii;

Now if you save and refresh, you will see you get a nice date in the format you want, except for one small problem. It’s dropping a day. Huh? I have no explanation for this behavior. So let’s try a var_dump.

<?php var_dump($date);?>

That produces the same date and since there is absolutely nothing in between, so either my assumption on how it is supposed to work is wrong or there’s a bug. I checked one other thing while I was at it. My existing solution for date input handles it fine, but if you put gibberish in, you get 01-01-1970 as the date. I’m eventually going to write some additional filters to fix this, but I’m living with it for now. So I was hoping that the date formatter would handle this for me. So I tried the following:

public function actionIndex()
          $date = "kldkjkjskjfksjdfksdjf, 2014";
         $date = Yii::$app->formatter->asDate($date, "Y-M-d");
         return $this->render('index', [
            'date' => $date

The point of this was to simulate input from a form and see what the date formatter would do:

Invalid Parameter – yii\base\InvalidParamException

'kldkjkjskjfksjdfksdjf, 2014' is not a valid date time value: DateTime::__construct(): Failed to parse time string (kldkjkjskjfksjdfksdjf, 2014) at position 0 (k): The timezone could not be found in the database

That is not handling the error in a way that I could use on an application.

Now if you remember at the beginning of this tutorial, I said I was using an example I found in the forum. It could be that I don’t understand how to use the formatter, so I checked the guide.

From what I see there, it does appear that the formatter takes two arguments, the date you are handing in and the format of the date, so unless I missed something, it should work the way I’m testing it.

Now normally, I would go back to the forum and post the problem, just to make sure I’m not the problem, but in this case, because of the Invalid Parameter exception, I put in an issue on Github. If it is a bug that needs to be fixed, the core Yii 2 team will make that determination. I went this route because there was already an existing problem with the DatePicker widget, so it made sense that they might want to look at this too.

After I submitted my request, while I was writing this tutorial, they closed the issue within minutes with the following comments:

1. you have to use the ICU format for date patterns, not PHP date() format, so yyyy-MM-dd would be a correct format in your case.
2. the date issue is a duplicate of #5448
3. the formatter is not meant to be used with user input directly. You should validate user input using the date validator first which will handle this case.

As a beginner, it’s difficult to know when to submit an issue and I would recommend going to the forum first, which is probably what I should have done in this case. Since the formatter isn’t intended to be used the way I want to use it, which is to convert time format from user input to another format, I’m going to abandon it completely for this purpose.

Not everything works out when you try it. This is why the test controller is so handy, I could test all this without mucking up my other code.

Anyway, at this point, you might be interested in my actual solution to the date format conversion, so I’m going to provide it.

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.


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