Yii 透過「應用程式層級」與「控制器層級」雙重設定,彈性決定預設進入點。
檔案: /protected/config/main.php
<?php
return array(
'name' => 'Auto',
'defaultController' => 'auto', // 對應 AutoController.php
);
此時訪問 index.php 將自動路由至 AutoController。
檔案: /protected/controllers/AutoController.php
class AutoController extends CController {
public $defaultAction = 'test'; // 預設動作
public function actionTest() {
// 預設進入邏輯
}
}
| 訪問 URL | 實際路由 | 說明 |
|---|---|---|
https://domain/index.php | index.php?r=auto/test | 雙重預設生效 |
https://domain/index.php?r=auto | index.php?r=auto/test | 僅指定 Controller,Action 套用預設 |
https://domain/index.php?r=auto/index | index.php?r=auto/index | 明確指定 Action,覆蓋預設 |
defaultController 指向 SPA 入口控制器defaultController 設定auto 對應 AutoController)public 且命名為 action* 格式善用雙層預設機制,能讓 URL 更乾淨,也便於專案初期快速原型開發。