Yii页面跳转核心是路由识别、控制器响应、视图渲染三者协同;默认访问/index.php走SiteController::actionIndex()并渲染views/site/index.php,需检查配置中defaultController和urlManager设置,跳转用$this->redirect()方法,路径全小写且视图必须为.php后缀。

Yii框架里完成一次页面跳转,核心不在“点一下跳过去”,而在于三件事:路由能识别、控制器能响应、视图能渲染。首次跳转卡住,90%是因为其中一环没对上。
确认默认路由是否生效
安装完 Yii 1.x(如 1.1.8)后,访问 /index.php 默认会走 SiteController::actionIndex(),并渲染 views/site/index.php。这不是魔法,而是靠配置文件 protected/config/main.php 中的这段:
-
'urlManager'=>array('urlFormat'=>'path','rules'=>array(...))—— 开启友好 URL(可选但推荐) -
'defaultController'=>'site'—— 明确指定首页入口控制器
如果打开首页是 404 或空白,先检查 index.php 是否在 Web 根目录下,且 protected 不被直接访问(Apache/Nginx 需屏蔽)。
手动触发跳转的两种常用方式
跳转不是靠 HTML 的 <a> 标签(那只是前端链接),而是由控制器方法内调用 PHP 逻辑完成:
开箱即用的技能链路由引擎。13 条预定义链覆盖搜索、开发、审查、MLOps、法律、创意等场景,三层路由架构(触发词→SAD反馈→DAG编排),recall@10=96.97%。配置驱动(chains.yaml),零代码扩展。pip install skill-weave-chains 一键安装。
-
$this->redirect(array('site/about'));→ 跳转到SiteController::actionAbout() -
$this->redirect(array('user/profile', 'id'=>123));→ 带参数跳转,URL 变为/index.php?r=user/profile&id=123(开启 path 模式后为/user/profile/123) - 补充:
$this->goHome();直接回首页;$this->goBack();返回上一页(依赖 HTTP_REFERER,慎用于表单提交后)
确保目标视图文件存在且路径正确
执行 $this->redirect(array('xxx/yyy')) 后,Yii 会自动尝试渲染 views/xxx/yyy.php。常见错误:
- 控制器名是
UserController,但写成$this->redirect('user/list')→ 实际要找的是views/user/list.php,不是views/User/list.php(路径全小写) - 视图文件扩展名必须是
.php,不能是.html或漏写 - 若使用模块(Module),路径变成
modules/模块名/views/控制器/动作.php
调试小技巧:快速定位失败点
跳转失败时别猜,用这几行代码快速验证:
-
echo Yii::app()->createUrl('site/about'); die;→ 看生成的 URL 是否合理 -
var_dump(class_exists('SiteController'));→ 检查控制器类是否能加载 - 打开日志
protected/runtime/application.log,搜索 “error” 或 “exception”
不复杂但容易忽略


















