yii 基础操作

1.生成项目
在cmd下进入framework目录。输入如下命令,可以在framework同级目录下生成hikecms项目。
D:\www\yii\framework> yiic webapp ../hikecms
D:\www\yii\framework> yiic.bat webapp ../hikecms

2.进入Gii。修改protected/config/main.php中的配置,将gii的配置打开。
 'modules'=>array(
  // uncomment the following to enable the Gii tool
  'gii'=>array(
   'class'=>'system.gii.GiiModule',
   'password'=>'123',
   // If removed, Gii defaults to localhost only. Edit carefully to taste.
   'ipFilters'=>array('127.0.0.1','::1'),
  ),
 ),


3.修改数据库为mysql数据库。注释原有的sqlite,打开
  /*
  'db'=>array(
   'connectionString' => 'sqlite:'.dirname(__FILE__).'/../data/testdrive.db',
  ),
  */
  // uncomment the following to use a MySQL database
  'db'=>array(
   'connectionString' => 'mysql:host=localhost;dbname=hikecms',
   'emulatePrepare' => true,
   'username' => 'root',
   'password' => 'root',
   'charset' => 'utf8',
    'tablePrefix' => 'hike_',//如果数据表有前缀的话 
  ),



4.增加module。在Gii的modules 的Module Generator生成backend,修改protected/config/main.php,在modules中增加一行
 'modules'=>array(
  // uncomment the following to enable the Gii tool
  'gii'=>array(
   'class'=>'system.gii.GiiModule',
   'password'=>'123',
   // If removed, Gii defaults to localhost only. Edit carefully to taste.
   'ipFilters'=>array('127.0.0.1','::1'),
  ),
  'backend',
 ),



5.增加后台的数据模型。先在数据表中增加对应的表格,如admin表,则在gii的Model Generator中生成模型
如果模型是后台的模型,需要指定对应的Model Path为application.modules.backend.models

6.增加后台的CRUD操作。在Gii的Crud Generator中进行操作,其中Model Class要指定为application.modules.backend.models.Admin,Controller ID要指定为backend/admin。

7.定义后台的layout文件和目录。比如后台大部分都是上面为菜单,左右两列式的。
修改modules/backend/BackendModule.php.
 public function beforeControllerAction($controller, $action)
 {
  // redefine layout
  $controller->layout='application.modules.backend.views.layouts.column2';
  if(parent::beforeControllerAction($controller, $action))

修改modules\backend\views\layouts\column2.php文件第一行
<?php $this->beginContent('application.modules.backend.views.layouts.main'); ?>

这样在main.php中定义总体的菜单,在column2中,实际上最好定义为main.php,然后在对应的controller中定义自己的layout,类似coumn2.

如果在具体的比如登录界面不需要具体的布局文件,可以将$this->layout = false;
 public function actionLogin()
 {
  $this->layout = false;



8.修改未登录默认的跳转地址。修改main.php配置文件
  'user'=>array(
   // enable cookie-based authentication
   'allowAutoLogin'=>true,
   'loginUrl'=>array('backend/admin/login'),
  ),



9.修改登录为admin表中进行鉴权。
修改protected\components\UserIdentity.php
class UserIdentity extends CUserIdentity
{
 private $_id;
 /**
  * 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.
  */
 public function authenticate()
 {
  $user=Admin::model()->find('LOWER(username)=?',array(strtolower($this->username)));
  if($user===null)
   $this->errorCode=self::ERROR_USERNAME_INVALID;
  else 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;
 }
}


修改protected\modules\backend\models\Admin.php
 /**
  * 鉴权
  * @param $password
  * @return bool
  */
 public function validatePassword($password) {
  if (md5($password) === $this->password) {
   return true;
  } elseif ($password === $this->password) {
   return true;
  }
  return false;
 }




10.配置后台不允许非登录用户访问,修改AdminController.php文件或对应的Controller文件的accessRules函数。
 public function accessRules()
 {
  return array(
   /*array('allow', // allow all users to perform 'index' and 'view' actions
    'actions'=>array('index','view'),
    'users'=>array('*'),
   ),*/
   array('allow', // allow authenticated user to perform 'create' and 'update' actions
    'actions'=>array('create','update','index','view','admin','delete'),
    'users'=>array('@'),
   ),
   /*array('allow', // allow admin user to perform 'admin' and 'delete' actions
    'actions'=>array('admin','delete'),
    'users'=>array('admin'),
   ),*/
   array('deny', // deny all users
    'users'=>array('*'),
   ),
  );
 }




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值