I am trying to build some Cronjobs handling.
I run my command (temp) from the CLI for testing:
php public/index.php send-private-messages-reminders direct
Therefor i created a Module with this routing in module.config.php
// cron route
'console' => array(
'router' => array(
'routes' => array(
'cronjobs' => array(
'type' => 'simple',
'options' => array(
'route' => 'send-private-messages-reminders <freq>',
'defaults' => array(
'controller' => 'Cron\Controller\SendPrivateMessagesReminders',
'action' => 'task'
),
)
),
),
),
),
Then i get my users and the messages for these users in my controller. I create my viewModel and prepare my mail:
$mailer = $this->getServiceLocator()->get('\SxMail\Service\SxMail');
$sxMail = $mailer->prepare();
$viewModel = new ViewModel();
For the layout i want to use a layout that i use in my Application also. So i define my layout in module.config.php
'view_manager' => array(
'template_map' => array(
'cron/mail/layout' => __DIR__ . '/../view/layout/mail.twig',
),
'template_path_stack' => array(
__DIR__ . '/../view',
),
),
and set my layout:
$sxMail->setLayout('cron/mail/layout');
Also there is more code for instance for the template:
$viewModel->setTemplate('mailtemplates/private-messages-reminders.twig');
and setting variables for that template:
$viewModel->setVariables(array( .... ));
So far so good. It works almost as expected. Only...
in my mail.twig template i use a ViewHelper for the urls, like this ie:
<img src="{{ url('mysite', {}, {force_canonical: true}) }}"images/merk-logo.png" />
In my Application everything is fine with the ViewHelper, but in my cron it is not. I get this error:
An exception has been thrown during the rendering of a template
("Request URI has not been set") in "application/mail/layout" at line ...
From here i'am at a loss. I can ask the routeMatch:
$routeMatch = $this->getServiceLocator()->get('Application')->getMvcEvent()->getRouteMatch();
this gives me:
object(Zend\Mvc\Router\Console\RouteMatch)#517 (3) {
["length":protected]=>
int(0)
["params":protected]=>
array(3) {
["controller"]=>
string(44) "Cron\Controller\SendPrivateMessagesReminders"
["action"]=>
string(4) "task"
["freq"]=>
string(6) "direct"
}
["matchedRouteName":protected]=>
string(8) "cronjobs"
}
but that is about it what i can find out. I have no clue what to do or how to solve the problem. I have been searching a lot, but the "solutions" so far are not detailed enough for me.
There is a missing route_match, but what to do about it?