Yiiフレームワーク(1.1.8)でチュートリアル。

PHPのYiiフレームワークってのを試してみたので記録。

1. 公式から.tar.gzをダウンロード

公式 http://www.yiiframework.com/

2. サーバー上に適当に配置

3. サーバーがYiiの要件を満たしているか確認

http://localhost/requirements/index.php にアクセス

4. テスト用のアプリを生成

以下の手順でコマンド入力し、テスト用のアプリケーションを生成。

$ cd (Yiiを配置したディレクトリ)
$ php framework/yiic.php webapp testdrive
Create a Web application under '/**省略**/testdrive'? [Yes|No]
yes[ent] ←入力

ざっと文字列が流れて

Your application has been created successfully under /**省略**/testdrive

となればOK

5. 確認

http://localhost/testdrive/ にアクセスして、
サンプルのアプリケーションが表示されれば問題なく生成できている。

6. データソースを設定

testdrive/protected/config/main.php

'db'=>array(
    'connectionString'=>'sqlite:protected/data/testdrive.db',
),

testdriveには既にデータがセットされたsqliteのDBが含まれています。

6. CRUD操作の実装

Giiを使ってCRUD操作を実装します。
さっきと同じmainの設定ファイルの以下の箇所を変更します。
testdrive/protected/config/main.php

'import'=>array(
    'application.models.*',
    'application.components.*',
),

'modules'=>array(
    'gii'=>array(
        'class'=>'system.gii.GiiModule',
        'password'=>'pick up a password here',
        'ipFilters'=>array('127.0.0.1','::1'),
    ),
),

今回使っているパッケージだと、上記import部分は既に書かれていて、
modules部分はコメントアウトされていました。
その為、modulesのコメントアウトを解除して、パスワードに任意の文字列をセット
さらにipFiltersの記述があった為、ローカルのipを指定しておいた。

次に、ブラウザから http://hostname/testdrive/index.php?r=gii にアクセスする。


さっき設定したパスワードを入力。


Model Generator のリンクをクリックし、モデル生成ページに移動。


モデルの情報を入力

table name : tbl_user
model class : User

previewを押すと生成されるコードファイルが見られるので、
適当に確認したら Generate ボタンを押下

The code has been generated successfully.

protected/models の下に User.php が生成されます。
エラーが出た場合はメッセージを読んで修正してください。
出るとしたらパーミッションの設定とかです。
modelsを777にすればおk

GiiのCrud Generatorを選択し、

ModelClass : User
ContorollerID : user

を入力してGenerate。

The controller has been generated successfully. You may try it now.

ブラウザから http://localhost/yii/testdrive/index.php?r=user にアクセスすれば動作確認ができます。
動作確認にはログインが必要ですが、 admin/admin でログイン可能です。

また、管理ページには http://localhost/yii/testdrive/index.php?r=user/admin でアクセスできます。