在Yii 1.1的引导程序(app / index.php,第13行)中,我们用Yii::createWebApplication($config)->run();
命令启动应用程序,createWebApplication
方法是(framework / YiiBase.php,第96行)
public static function createWebApplication($config=null)
{
return self::createApplication('CWebApplication',$config);
}
这样createWebApplication
方法是(framework / YiiBase,Line 123)
public static function createApplication($class,$config=null)
{
return new $class($config);
}
所以createApplication('CWebApplication',$config)
解释为像new CWebApplication($config);
这样的东西,哼唱?但是在CWebApplication
课程中,我们没有任何构造函数! (framework / yiilite.php,第1622行)。
现在的问题是: WebApplication如何在$config
变量上获取配置并使自己适应它?
答案 0 :(得分:1)
下面的函数将返回<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Integer nec odio. Praesent libero. Sed cursus ante dapibus diam. Sed nisi. Nulla quis sem at nibh elementum imperdiet. Duis sagittis ipsum. Praesent mauris. Fusce nec tellus sed augue semper porta. Mauris massa. Vestibulum lacinia arcu eget nulla. Class aptent taciti sociosqu ad litora torquent per conubia nostra, per inceptos himenaeos. Curabitur sodales ligula in libero. Sed dignissim lacinia nunc.
<br />
<br />
Curabitur tortor. Pellentesque nibh. Aenean quam. In scelerisque sem at dolor. Maecenas mattis. Sed convallis tristique sem. Proin ut ligula vel nunc egestas porttitor. Morbi lectus risus, iaculis vel, suscipit quis, luctus non, massa. Fusce ac turpis quis ligula lacinia aliquet. Mauris ipsum. Nulla metus metus, ullamcorper vel, tincidunt sed, euismod in, nibh. Quisque volutpat condimentum velit. Class aptent taciti sociosqu ad litora torquent per conubia nostra, per inceptos himenaeos.</p>
return new CWebApplication($config);
如果检查CWebApplication类扩展CApplication, CWebApplication.php
public static function createApplication($class,$config=null)
{
return new $class($config);
}
在CApplication中,您将找到初始化的构造函数方法 class CWebApplication extends CApplication
{
...
framework/base/CApplication.php
CWebApplication继承了CApplication,因此它也继承了Constructor方法。 为了得到这个想法,你可以查看下面的例子。
修改
abstract class CApplication extends CModule
{
....
public function __construct($config=null)
{
Yii::setApplication($this);
// set basePath at early as possible to avoid trouble
if(is_string($config))
$config=require($config);
if(isset($config['basePath']))
{
$this->setBasePath($config['basePath']);
unset($config['basePath']);
}
else
$this->setBasePath('protected');
Yii::setPathOfAlias('application',$this->getBasePath());
Yii::setPathOfAlias('webroot',dirname($_SERVER['SCRIPT_FILENAME']));
if(isset($config['extensionPath']))
{
$this->setExtensionPath($config['extensionPath']);
unset($config['extensionPath']);
}
else
Yii::setPathOfAlias('ext',$this->getBasePath().DIRECTORY_SEPARATOR.'extensions');
if(isset($config['aliases']))
{
$this->setAliases($config['aliases']);
unset($config['aliases']);
}
$this->preinit();
$this->initSystemHandlers();
$this->registerCoreComponents();
$this->configure($config);
$this->attachBehaviors($this->behaviors);
$this->preloadComponents();
$this->init();
}
abstract class CApplication
{
protected $config=array();
public function __construct($config=null) {
echo "CApplication::construct() called";
// DO anyting with config arrray which is from /protected/config/main.php
//$this->config = $config;
}
}