Yii Framework Forum: Getting everything Yii application offers without creating one - Yii Framework Forum

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • This topic is locked

Getting everything Yii application offers without creating one Rate Topic: -----

#1 User is offline   Vince. 

  • Advanced Member
  • PipPipPip
  • Yii
  • Group: Members
  • Posts: 432
  • Joined: 29-December 08
  • Location:Los Angeles,CA

Posted 18 October 2009 - 07:18 AM

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.
Vince Gabriel, Lead Developer.
Posted ImagePosted ImagePosted Image
1

#2 User is offline   qiang 

  • Yii Project Lead
  • Yii
  • Group: Yii Dev Team
  • Posts: 5,871
  • Joined: 04-October 08
  • Location:DC, USA

Posted 18 October 2009 - 08:45 AM

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

0

#3 User is offline   qiang 

  • Yii Project Lead
  • Yii
  • Group: Yii Dev Team
  • Posts: 5,871
  • Joined: 04-October 08
  • Location:DC, USA

Posted 18 October 2009 - 08:47 AM

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)
0

#4 User is offline   Vince. 

  • Advanced Member
  • PipPipPip
  • Yii
  • Group: Members
  • Posts: 432
  • Joined: 29-December 08
  • Location:Los Angeles,CA

Posted 18 October 2009 - 10:36 AM

Yep that did it. Thanks.
Vince Gabriel, Lead Developer.
Posted ImagePosted ImagePosted Image
0

#5 User is offline   huzng2h 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 7
  • Joined: 22-August 10
  • Location:Hanoi, Vietnam

Posted 31 August 2010 - 02:33 AM

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

#6 User is offline   mbi 

  • Master Member
  • PipPipPipPip
  • Yii
  • Group: Members
  • Posts: 604
  • Joined: 08-May 09

Posted 09 December 2011 - 05:02 AM

This also works for me

require_once $yii;
$app = Yii::createWebApplication($config);
$app->runController('site/index');

0

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • This topic is locked

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users