YiiFramework入门知识点总结(图文教程)

创建Yii应用骨架

web为网站根目录
yiic webapp /web/demo

通过GII创建model和CURD时需要注意

1、Model Generator 操作

即使在有表前缀的情况下,Table Name中也要填写表的全名,即包括表前缀。如下图:

YiiFramework入门知识点总结(图文教程)

2、Crud Generator 操作

该界面中,Model Class中填写model名称。首字母大写。也可参照在生成model时,在proctected/models目录中通过model generator生成的文件名。如下图:

YiiFramework入门知识点总结(图文教程)

如果对news、newstype、statustype这三个表生成CURD控制器,则在Model Generator中,在Model Class中输入:News、newsType、StatusType。大小写与创建的文件名的大小写相同。如果写成NEWS或NeWs等都不可以。

创建模块注意事项

通过GII创建模块,Module ID一般用小写。无论如何,这里填写的ID决定main.php配置文件中的配置。如下:

'modules'=>array( 'admin'=>array(//这行的admin为Module ID。与创建Module时填写的Module ID大写写一致 'class'=>'application.modules.admin.AdminModule',//这里的admin在windows os中大小写无所谓,但最好与实际目录一致。 ), ),

路由

system表示yii框架的framework目录
application表示创建的应用(比如d:\wwwroot\blog)下的protected目录。
application.modules.Admin.AdminModule
表示应用程序目录(比如:d:\wwwroot\blog\protected)目录下的modules目录下的Admin目录下的AdminModules.php文件(实际上指向的是该文件的类的名字)
system.db.*
表示YII框架下的framework目录下的db目录下的所有文件。

控制器中的accessRules说明

/** * Specifies the access control rules. * This method is used by the 'accessControl' filter. * @return array access control rules */ public function accessRules() { return array( array('allow', // allow all users to perform 'index' and 'view' actions 'actions'=>array('index','view'),//表示任意用户可访问index、view方法 'users'=>array('*'),//表示任意用户 ), array('allow', // allow authenticated user to perform 'create' and 'update' actions 'actions'=>array('create','update'),//表示只有认证用户才可操作create、update方法 'users'=>array('@'),//表示认证用户 ), array('allow', // allow admin user to perform 'admin' and 'delete' actions 'actions'=>array('admin','delete'),//表示只有用户admin才能访问admin、delete方法 'users'=>array('admin'),//表示指定用户,这里指用户:admin ), array('deny', // deny all users 'users'=>array('*'), ), ); }

看以上代码注释。

user: represents the user session information.详情查阅API:CWebUser
CWebUser代表一个Web应用程序的持久状态。
CWebUser作为ID为user的一个应用程序组件。因此,在任何地方都能通过Yii::app()->user 访问用户状态

public function beforeSave() { if(parent::beforeSave()) { if($this->isNewRecord) { $this->password=md5($this->password); $this->create_user_id=Yii::app()->user->id;//一开始这样写,User::model()->user->id;(错误) //$this->user->id;(错误) $this->create_time=date('Y-m-d H:i:s'); } else { $this->update_user_id=Yii::app()->user->id; $this->update_time=date('Y-m-d H:i:s'); } return true; } else { return false; } }

getter方法或/和setter方法

<?php /** * UserIdentity represents the data needed to identity a user. * It contains the authentication method that checks if the provided * data can identity the user. */ class UserIdentity extends CUserIdentity { /** * Authenticates a user. * The example implementation makes sure if the username and password * are both 'demo'. * In practical applications, this should be changed to authenticate * against some persistent user identity storage (e.g. database). * @return boolean whether authentication succeeds. */ private $_id; public function authenticate() { $username=strtolower($this->username); $user=User::model()->find('LOWER(username)=?',array($username)); if($user===null) { $this->errorCode=self::ERROR_USERNAME_INVALID; } else { //if(!User::model()->validatePassword($this->password)) if(!$user->validatePassword($this->password)) { $this->errorCode=self::ERROR_PASSWORD_INVALID; } else { $this->_id=$user->id; $this->username=$user->username; $this->errorCode=self::ERROR_NONE; } } return $this->errorCode===self::ERROR_NONE; } public function getId() { return $this->_id; } }

model/User.php

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:http://www.heiqu.com/64fb5ccb0b5d6848244cdf37d7fe9431.html