純oop框架php
官網:http://www.yiiframework.com/ 中文網站:http://www.yiichina.com/css
打開cmd找到yii目錄裏面的framework輸入yiic webapp 項目名稱(好比yiic webapp ../one)one就是這個項目的名稱,mysql
class IndexController extends Controller { /** * 默認方法 */ public function actionIndex() { echo 666; } }
訪問 例如:http://localhost/yii-1.1.18.018a89/study/index.php?r=index/indexjquery
r是route縮寫,後面第一個爲控制器,第二個爲方法web
在cofig裏面的main.php增長代碼以下sql
//設置默認控制器 'defaultController' => 'Index',
$this->render('index');會載入佈局
$this->renderPartial('index');不會載入佈局,也不會加載框架自帶的jquery
$this->render('index',$data);
<?php foreach($article as $v): ?> <li><?php echo $v->title;?></li> <?php endforeach ?>
用render自動加載公共區域並在views/layouts下面的文件就是佈局文件數據庫
位置:components/Controller.phpsession
<link href="<?php echo Yii::app()->request->baseUrl ?>/assets/index/css/index.css" rel="stylesheet" />
在protected目錄下創建functions.php文件app
引入函數,在index.php下加入如下代碼框架
include_once "./protected/functions.php";
將config下main.php裏modules註釋打開
訪問 http://localhost/yii-1.1.18.018a89/study/index.php?r=gii ,點擊Module Generator,以下圖
須要訪問新建立的admin,須要在main.php,gii下面加入admin以下圖
訪問地址例如:http://localhost/yii-1.1.18.018a89/study/index.php?r=admin/index/test
前置條件以下:
小物件使用具體代碼以下:
<?php $form = $this->beginWidget('CActiveForm') ?> <?php echo $form->textField($loginForm, 'username', array('id'=>'userName')) ?> <?php echo $form->passwordField($loginForm, 'password', array('id'=>'psd')) ?> <?php echo $form->textField($loginForm, 'captcha', array('id'=>'verify')) ?> <input type="submit" id="sub" value=""/> <!-- 驗證碼 --> <div class="captcha"> <?php $this->widget('CCaptcha',array('showRefreshButton'=>false,'clickableImage'=>true,'imageOptions'=>array('alt'=>'點擊換圖','title'=>'點擊換圖','style'=>'cursor:pointer'))); ?> </div> <?php $this->endWidget() ?>
<ul id="peo"> <li class="error"><?php echo $form->error($loginForm,'username') ?></li> </ul> <ul id="psd"> <li class="error"><?php echo $form->error($loginForm,'password') ?></li> </ul> <ul id="ver"> <li class="error"><?php echo $form->error($loginForm,'captcha') ?></li> </ul>
注意:小物件裏面用到的名稱須要在對應的model層進行定義
驗證碼控制器使用方法
public function actions(){ return array( 'captcha' => array( 'class' => 'system.web.widgets.captcha.CCaptchaAction', 'height' => 25, 'width' => 80, 'minLength'=> 4, 'maxLength'=> 4 ), ); }
若須要驗證碼變化,須要到framework/web/widgets/captcha/CCaptchaAction類裏面修改核心類代碼
設置驗證規則
在對應的model層進行規則驗證
public function rules()
{
return array(
// username and password are required
array('username', 'required', 'message'=>'用戶名必須填寫'),
array('password', 'required', 'message'=>'密碼必須填寫'),
// rememberMe needs to be a boolean
array('rememberMe', 'boolean'),
// password needs to be authenticated
array('password', 'authenticate'),
array('captcha', 'captcha', 'message'=>'驗證碼錯誤')
);
}
在config下面的main.php中配置以下代碼,下面配置在framework/db/CDbConnection.php中能夠找到
'db'=>array( 'connectionString' => 'mysql:host=127.0.0.1;dbname=blog_test', 'emulatePrepare' => true,//pdo擴展 'username' => 'root', 'password' => '', 'charset' => 'utf8', 'tablePrefix' => 'hd_', 'enableParamLogging' => true//開啓調試信息的sql語句具體值信息 ),
使用var_dump(Yii::app()->db);判斷是否鏈接數據庫成功
必須有model和tabaleName方法
<?php
class User extends CActiveRecord{
public $password1;
public $password2;
public static function model($className = __CLASS__){
return parent::model($className);
}
public function tableName(){
return "{{admin}}";
}
public function attributeLabels(){
return array(
'password' => '原始密碼',
'password1' => '新密碼',
'password2' => '確認密碼'
);
}
public function rules(){
return array(
array('password', 'required', 'message'=>'原始密碼必填'),
array('password', 'check_passwd'),
array('password1', 'required', 'message'=>'新密碼必填'),
array('password2', 'required', 'message'=>'確認密碼必填'),
array('password2', 'compare', 'compareAttribute'=>'password1', 'message'=>'兩次密碼不相同'),
);
}
public function check_passwd(){
$userInfo = $this->find('username=:name', array(':name'=>Yii::app()->user->name));
if(md5($this->password) != $userInfo->password){
$this->addError('password', '原始密碼不正確');
}
}
}
$this->redirect(array('default/index'));
<p class="out"> <span class="out_bg">    </span>  <a href="<?php echo $this->createUrl('login/out',array("id" => 1)) ?>" target="_self">退出</a> </p>
//設置session Yii::app()->session['logintime'] = time(); //獲取session Yii::app()->session['logintime']
//退出登陸 Yii::app()->session->logout(); //清除文件 Yii::app()->session->destroy(); //銷燬變量 Yii::app()->session->clear();
控制器中代碼以下
if($userModel->updateByPk($userInfo->uid, array('password'=>$password))){ Yii::app()->user->setFlash('success', '修改密碼成功'); }
視圖中代碼以下
<?php if(Yii::app()->user->hasFlash('success')){ echo Yii::app()->user->getFlash('success'); } ?>
在main.php配置文件中,以下
'log'=>array( 'class'=>'CLogRouter', 'routes'=>array( array( 'class'=>'CFileLogRoute', 'levels'=>'error, warning', ), // uncomment the following to show log messages on web pages array( 'class'=>'CWebLogRoute', ), ), ),
增的時候須要new模型
$model = new Model();
$model->attributes = $_POST['user'];
$model->save();
save方法在new模型的時候是增長,在Model::model()的時候是修改
1.查詢單條
查詢單條 find(),如:find("username=:name",array(":name"=>"admin"));
經過主鍵查詢 findByPK(),如:findByPK(1);
經過sql查詢 findBySql(),如:findBySql("SELECT * FROM {{admin}}");
2.查詢多條
查詢多條信息 findAll(),如:findAll("username=:name",array(":name"=>"admin"));
經過多個主鍵查詢 findAllByPK(),如:findAllByPK(array(1,2));
經過sql查詢多條 findAllBySql(),如:findAllBySql("SELECT * FROM {{admin}}");
$model = User::model();
$model = attributes = $_POST['user'];
$model->save();
此時save是修改
$model = User::model();
$model->deleteByPK($id);