Console app and SwiftMailer

Hello!

I have created a code to send notifications to users. I want to do it automatically through cron. To have this done, I have created a console controller. When trying to launch it I get the following exception:


2015-07-13 12:19:53 [-][-][-][error][yii\base\UnknownPropertyException] exception 'yii\base\UnknownPropertyException' with message 'Getting unknown property: yii\console\Request::url' in C:\xampp\htdocs\appname\vendor\yiisoft\yii2\base\Component.php:143

What’s wrong with the console/swiftmailer config? The same code works perfectly fine when requested through regular webcontroller…

Some more details I found…

In my email template I’m using the GridView to present the result.

So looks like the error is related to GridView…

Stack trace:

#0 C:\xampp\htdocs\appname\vendor\yiisoft\yii2\grid\GridView.php(317): yii\base\Component->__get(‘url’)

#1 C:\xampp\htdocs\appname\vendor\yiisoft\yii2\grid\GridView.php(278): yii\grid\GridView->getClientOptions()

#2 C:\xampp\htdocs\appname\vendor\yiisoft\yii2\base\Widget.php(97): yii\grid\GridView->run()

I just came across this issue. It looks like url is only used to generate the $filterUrl. In your gridview, if like me you are rendering this to go in an email for filters are not required, the add filterUrl to your Gridview config:




    <?= GridView::widget([

        'dataProvider' => $dataProvider,

        'filterModel' => null,

        'filterUrl' => 'http://nowhere.com/',

        ....



This property is still required, even when filterModel is null (looks like a bug to me - I will do a fix when I get time).

I would guess the next issues you hit are due to Gridview thinks it needs assets (looks like it does not if sorting and filtering are not used). I fixed this by setting the @web alias before calling renderPartial:




    \Yii::setAlias('web', \Yii::$app->urlManager->baseUrl);



Why would one use GridView in an email template? None of the features it offers (pagination, filtering, sorting, AJAX) work in email context and it causes much more problems than it solves. A HTML table can be generated with a foreach loop and a few lines of HTML code in your email templates.