Autoloading Yii application classes in external scripts

Notice: This post is more than a year old. It may be outdated.

Sometimes I want to have access to all of my classes in a Yii app externally. For example, I would like to run an external PHP script with cron and I would like to use my application classes. To achieve that, I created a separate classloader file.

<?php

// change the following paths if necessary
$yii=dirname(__FILE__).'/../vendor/yiisoft/yii/framework/yii.php';
$config=dirname(__FILE__).'/config/console.php';
$composer = dirname(__FILE__) . '/../vendor/autoload.php';

require_once($composer);
require_once($yii);
Yii::createConsoleApplication($config);

This creates a Yii console application and in addition autoloads all classes managed by Composer. Now you can use your application classes for example in a cron script.

comments powered by Disqus