Getting everything Yii application offers without creating one

I was wondering if there was a way to initiate Yii without the MVC pattern. Just load the config, Initiate everything for use in files such as a web service for other customers that would like to communicate with the system without the need to create a new module/controller/action something simple.

Currently my YII index.php file is:


<?php


// Define root directory

defined('ROOT_PATH') or define('ROOT_PATH', dirname(__FILE__) . '/');


// remove the following line when in production mode

defined('YII_DEBUG') or define('YII_DEBUG', true);


// Shortcut for DIRECTORY_SEPERATOR

defined('DS') or define('DS', DIRECTORY_SEPARATOR);


// Load zend auto loader

//require_once "Zend/Loader/Autoloader.php";

//Zend_Loader_Autoloader::getInstance();


// include Yii bootstrap file

require_once(ROOT_PATH.'Library/Yii/yii.php');


// include Init Data

require_once('init.php');


// Set path alias to the library, sources, classes and zend

Yii::setPathOfAlias('library', ROOT_PATH . 'Library');

Yii::setPathOfAlias('sources', ROOT_PATH . 'Library/Sources');

Yii::setPathOfAlias('zend', ROOT_PATH . 'Library/Zend');

Yii::setPathOfAlias('classes', ROOT_PATH . 'Library/Sources/Classes');


// Load Config file

$configFile = ROOT_PATH.'Protected/config/main.php';


// create a Web application instance and run

Yii::createWebApplication($configFile)->run();


exit();

I would like to have something like:


<?php


// Define root directory

defined('ROOT_PATH') or define('ROOT_PATH', dirname(__FILE__) . '/');


// remove the following line when in production mode

defined('YII_DEBUG') or define('YII_DEBUG', true);


// Shortcut for DIRECTORY_SEPERATOR

defined('DS') or define('DS', DIRECTORY_SEPARATOR);


// Load zend auto loader

//require_once "Zend/Loader/Autoloader.php";

//Zend_Loader_Autoloader::getInstance();


// include Yii bootstrap file

require_once(ROOT_PATH.'Library/Yii/yii.php');


// include Init Data

require_once('init.php');


// Set path alias to the library, sources, classes and zend

Yii::setPathOfAlias('library', ROOT_PATH . 'Library');

Yii::setPathOfAlias('sources', ROOT_PATH . 'Library/Sources');

Yii::setPathOfAlias('zend', ROOT_PATH . 'Library/Zend');

Yii::setPathOfAlias('classes', ROOT_PATH . 'Library/Sources/Classes');


// Load Config file

$configFile = ROOT_PATH.'Protected/config/main.php';


// create a Web application instance and run

Yii::createWebApplication($configFile);


// Now i can access everything i have set in the config file such as the db class, caches, and others


print_r(Yii::app()->db->createCommand("something"));

Thanks.

Yes, what you did is right. That is, if you don’t need MVC, you only need the following:




require_once('path/to/yii.php');

Yii::createWebApplication($config);

// you can access Yii::app() now as usual



If you don’t need Yii::app(), you can even drop the line of creating the application instance.

By only including yii.php, you will be able to use the whole Yii library (e.g. Yii DAO)

Yep that did it. Thanks.

this one is a very nice article. I really need it. Many thanks!

This also works for me




require_once $yii;

$app = Yii::createWebApplication($config);

$app->runController('site/index');