Wednesday, February 26, 2014

Yii and non-Yii

Blending Yii and non-Yii applications together

If you want to link Yii application with non-Yii application, the best option is to re-write the existing application in Yii.

Definitely you can initiate HTTP requests to invoke Yii actions, but it creates TCP/IP connections which are costly. If both applications reside in the same web server, you can use below method.


Create a bootstrap (this code segment can be placed inside anywhere in current non-Yii application):
<?php
// change the following paths if necessary
$yii='yii/framework/yii.php';
$config=dirname(__FILE__).'/protected/config/application.php';

// remove the following lines when in production mode
//defined('YII_DEBUG') or define('YII_DEBUG',true);
// specify how many levels of call stack should be shown in each log message
defined('YII_TRACE_LEVEL') or define('YII_TRACE_LEVEL',3);

require_once($yii);

// `Application.php` must exist in the same level of THIS php file
$yiiapp = Yii::createApplication('Application', $config);

// Call methods inside `Application.php`
$yiiapp->doAction1();
$yiiapp->doAction2();

Create the wrapper `Application.php` in the same dir-level as the above bootstrap code segment is (any name can be used; but need to alter Yii::createApplication(...) above)
<?php
/**
 * NOTE:
 *   - `Yii::app()` is available from here.
 */
class Application extends CApplication
{
  public function init()
  {
  }

  /**
   * Yii::createApplication(...)->run() executes this method
   */
  public function processRequest()
  {
  }


  public function doAction1()
  {
  }

  public function doAction2()
  {
  }
}