函数:Yaf_Dispatcher::setDefaultAction()
适用版本:Yaf框架版本2.0.0以上
用法: Yaf_Dispatcher::setDefaultAction(string $action) : bool
该方法用于设置默认的控制器动作(Action)。当没有指定具体的控制器动作时,Yaf框架会自动调用该默认动作。
参数:
- $action:要设置为默认动作的字符串值。
返回值:
- 如果设置成功,则返回true;否则返回false。
示例:
<?php
// 在Bootstrap文件中设置默认动作
class Bootstrap extends Yaf_Bootstrap_Abstract {
public function _initDefaultAction(Yaf_Dispatcher $dispatcher) {
$dispatcher->setDefaultAction('index'); // 设置默认动作为"index"
}
}
// 控制器类
class IndexController extends Yaf_Controller_Abstract {
public function indexAction() {
echo "默认动作";
}
public function otherAction() {
echo "其他动作";
}
}
// 启动Yaf框架
$app = new Yaf_Application("/path/to/config.ini");
$app->bootstrap()->run();
?>
在上述示例中,我们在Bootstrap文件中通过调用Yaf_Dispatcher::setDefaultAction()
方法将默认动作设置为index
。当用户访问没有指定具体动作的控制器时,Yaf框架会自动调用IndexController
的indexAction()
方法,并输出"默认动作"。
请注意,此示例仅为演示目的,并不包含完整的Yaf框架应用程序结构。你需要根据实际情况进行适当的配置和调整。