除了yii框架,自包含的一个php文件




<?php

//`mkdir -p protected/controllers`;

// `touch protected/controllers/SiteController.php`;


include 'yii/framework/yii.php';

class SiteController extends CController{

  function actionIndex(){

   echo 'hello world';

}

}

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

?>



不需要


 `mkdir -p protected/controllers`;

  `touch protected/controllers/SiteController.php`;

这两步,那么就是自包含的one file 应用

smart.

yii-1.1.10 patch: just modify "yii/framework/web/CWebApplication.php" on line 339 below,add "patch 1".

now,it become true that controller can be in the index.php without "protected/controlers" directory




$className=ucfirst($id).'Controller';//line 339

//patch 1

				if(class_exists($className,false) && is_subclass_of($className,'CController'))

				{

					$id[0]=strtolower($id[0]);

					return array(

						new $className($controllerID.$id,$owner===$this?null:$owner),

						$this->parseActionParams($route),

					);

				}

//patch 1 end

			$classFile=$basePath.DIRECTORY_SEPARATOR.$className.'.php';



demo:controller and model ,both in the index.php working fine with sqlite:


<?php

include 'yii/framework/yii.php';


//Controller

class SiteController extends CController{

    function actionIndex(){

      //Yii::app()->db->createCommand("create table pic(id integer primary key autoincrement,name)")->execute(); 

       $pic=new Pic;

       $pic->name=rand();

       $pic->save();

       $data = Pic::model()->findAll();

      echo "hello world";

      print_r($data);

    }

}

//model

class Pic extends CActiveRecord{


   static function model($c=__CLASS__){return parent::model($c);}

    

}

//run!

Yii::createWebApplication(

array("components"=>array('db'=>array(

 'connectionString'=>'sqlite:self.db'

)))

)->run();