Circumvent CConsoleApp's limitations with a web service?

My app has a an event table, and corresponding model, and an EventProcessor CComponent. Events can be instantiated and processed synchronously. Or they can be saved to the table and run asynchronously by another process, either immediately or in the future by a cron job. EventProcessor renders views for some events, which can be decorated for browser display (sync processing only) or HTML email (sync or async both work).

I ran into trouble writing the console app for async processing. CConsoleApplication lacks stuff EventProcessor needed. So I started rewriting those parts of it with branches to deal with web vs. cli contexts, instantiating and using a controller, writing url creators that don’t need Yii::app()->request, resolving view file references, …

Eventually I stopped. It felt wrong. And I was concerned about breaking functionality that already worked.

CConsoleApplication is great for mapping the CLI into actions, etc. But it doesn’t do V in MVC and some of the C is different too.

I considered writing a hybrid of CConsoleApplication and CWebApplication. It seemed more work that the previously abandoned architecture but with the same problems plus new maintenance issues.

After discussions on #yii (Freenode) came another idea: Rewrite the console app as a web service and client. The console app would run cURL instead of calling methods in EventProcessor, which the web service would call instead.

From one point of view this is perverse. I don’t need a web service. Its just to provide an interface between the CWebApplication class and a console app. (Plus I need to deal with security on that interface.) But it might be the easiest architecture to maintain.

what do you think? Good architecture? Or is there a better one?

Is this a very exotic use of Yii? Or is my question too long for anyone to read?


You write a component that uses all 3 of MV&C and you want to use it in a webapp and in a console app. How do you make all the usual Yii stuff like render, createUrl, resolveView/File, etc. work the same in both contexts?

or is it a lost cause?



resolveView/File: ??

One way I’ve solved the view problem is to generate the view and save the resulting html into a DB column. The console can then retrieve it.

foreach ($users as $user)


    $email = new EmailQueue();

    $email->from_email = Yii::app()->params['adminEmail'];

    $email->from_name = Yii::app()->params['emailName'];

    $email->subject = 'MENA Investment Network Project Opportunity Matching';

    $email->to_email = $user->email_address;

    $email->date_published = new CDbExpression('NOW()');

    $email->message = Yii::app()->controller->renderPartial('//mail/notify/create', array(

        'company_name' => ($user->company != "") ? $user->company : $user->username,

        'title' => $project->name,

        'project_id' => $project->id,

        'title' => $project->name,

        'create_date' => date('o-m-d', strtotime($project->created_date)),

        'category' => $project->category->name,

        'summary' => $project->short_description,

        'send_date' => date('o-m-d', time()),

        ), true);



You could also write a module and call it async’ly through AJAX.