Using Laminas in a cron job

Sooner or later, every bigger project needs cron jobs for background operations, maintenance or some periodical reports etc. If you are beginning with Laminas framework, it maight not be straight obvious how to use Laminas in a cron file, so you can access services and configured view renderer and other properties.

For example ... let’s have cron file daily-report.php that should select some data from database, create e-mail body and send e-mail. This could be useful for daily e-mail reports.

Directory structure on server could look like this, with Document root pointing to /public folder.

├── CRON.1h
│   └── daily-report.php
├── CRON.week
│   └── data
│       └── log
│   └── public
│       └── index.php

In Laminas MVC skeleton index.php small update is needed where Application::init() is placed. Constant IS_CLI will be defined in a cron file.

File index.php

// Run the application!
if (! defined('IS_CLI') || IS_CLI == false) {

In the cron file we request model service and view renderer from our application, create e-mail body and pass it to Laminas mailer. Note, it is a good habit to log the process, so we have control that cron script runs regularly.

Cron file daily-report.php

use Laminas\Mvc\Application;

define('IS_CLI', true);
define('APP_ENV', 'development');

$webRoot = __DIR__ . '/../';
require $webRoot . '/public/index.php';

$application = Application::init($appConfig);

$writer = new Laminas\Log\Writer\Stream($webRoot . '/data/log/cron_daily_report.log');
$logger = new Laminas\Log\Logger();

try {
    $dateTime = new DateTime();
    $modelUsersActions = $application->getServiceManager()->get(User\Model\UsersActionsLogTable::class);
    $viewRenderer = $application->getServiceManager()->get('ViewRenderer');

    $htmlBody = $viewRenderer->render('application/cron/messageUserReport',
            'report' => $modelUsersActions->dailyUserAction($dateTime->format('Y-m-d'))

    if(empty($htmlBody)) {
        throw new Exception('E-mail body is empty.');

    $partHtml = new Laminas\Mime\Part($htmlBody);
    $partHtml->type = Laminas\Mime\Mime::TYPE_HTML;
    $partHtml->charset = 'UTF-8';

    $mimeMessage = new Laminas\Mime\Message();

    $message = new Laminas\Mail\Message();
    $message->setSubject('Daily report ' . $dateTime->format('d. m.'));

    if (false === $message->isValid()) {
        throw new \UnexpectedValueException('E-mail message is not valid. Mail failed.');

    $transport = new Laminas\Mail\Transport\Sendmail();

    $logger->info('Report send.');

} catch (\Exception $e) {
    $logger->crit($e->getMessage() . ' -> ' . $e->getTraceAsString());