blogデモにユーザー管理機能を設ける
blogデモにユーザー管理機能を追加したときのメモ。gii-Crud GeneratorでUserモデルメンテナンス機能を生成し、生成したコードを調整します。
Userモデルメンテナンス機能を生成する
gii を有効にする
ついでにgiiにたどり着けるよう urlManager も調整する。なんか勘違いしていた。
protected/config/main.php
<?php 'modules'=>array( 'gii'=>array( 'class'=>'system.gii.GiiModule', 'password'=>'gii', // If removed, Gii defaults to localhost only. Edit carefully to taste. ), ),
user の crud を作成
http://path/to/index.php/gii へアクセスしuser の crud を作成する。
giiで生成したコードを調整する
password, saltは表示したくない。saltは入力したくないので次を修正する。
protected/views/user/_form.php
saltは入力不要。salt をカット
protected/views/user/_search.php
saltは表示不要。salt をカット
protected/views/user/_view.php
password, saltは表示不要。password, salt をカット
protected/views/user/admin.php
password, saltは表示不要。password, salt をカット
protected/views/user/view.php
password, saltは表示不要。password, salt をカット
protected/models/user.php
saltは入力しないので rules から salt をカット。beforeSave を追加する。
<?php public function rules() { return array( array('username, password, email', 'required'), array('username, password, email', 'length', 'max'=>128), array('profile', 'safe'), ); } protected function beforeSave() { $this->salt = $this->generateSalt(); $this->password = $this->hashPassword($this->password, $this->salt); return true; }
使い方
一覧表示 : http://path/to/index.php/user
詳細表示 : http://path/to/index.php/user/view?id=999
作成 : http://path/to/index.php/user/create
変更 : http://path/to/index.php/user/update?id=999
削除 : http://path/to/index.php/user/delete?id=999
コンテキストメニュー等の修正はパス。