Yii

Yii デフォルトコントローラ/アクションの変更

「http://xxx.com/」とアクセスされた際、「IndexController/actionIndex」がデフォルト実行される。これを「GameController/actionPlay」に変更する方法は、「config/main.php」と「GameController.php」を以下のようにする。

config/main.php
<?php
return array(
	'language' => 'ja',
	'defaultController' => 'game',
	'components' => array(
		'urlManager' => array(
			'urlFormat' => 'path',
		),
	),

「’language’=>’ja’」の指定で、フレームワークからのエラーメッセージが日本語出力される。「components」の「urlFormat」指定は、「http://xxx.com/index.php/game/play」のパス形式でアクセスするための指定。

controllers/GameController.php
<?php
class GameController extends CController
{
	public $defaultAction = 'play';

	public function actionPlay()
	{

パス形式を「http://xxx.com/game/play」のようにアクセスするには、「urlManager」に「’showScriptName’=>false」を追加し、.htaccess で 以下のように rewrite_mod を利用するとよい。

.htaccess
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^.*$ index.php [L]

(yii-1.1.0.r1700)

追補
さくらの共用サーバーでサブドメイン(sub)のサブディレクトリ(subdir)で、「www/sub/subdir/index.php」の場合、上の設定ではwebサーバーのリクエストが「/sub/subdir/index.php」となってしまい、404エラーとなった。そこで「RewriteBase /subdir 」としたところ、動作するようになった。

100326 (yii-1.1.1.r1907)