请问controller的缺省Action是什么?

#1 yanjingtao

在PATH_INFO模式下,我要通过xxx.com/page/2这样来访问第二页,page.class.php是controller,那么2应该怎么获得呢?貌似会直接报路由错误。

2011-12-25 12:55:27

#2 jake

虽然是看不懂,但是可以建议你看看手册,能否解决你的问题 http://www.speedphp.com/interaction-diy-rewrite.html

另外,对于页码的使用,可以用站内搜索,论坛内有许多帖子都有实例代码。
当然,如果纯是提问,默认action是什么,默认action叫index
http://www.speedphp.com/hello-world.html

2011-12-25 14:26:34

#3 yanjingtao

问题的意思是,如果某个action不存在,那么,controller是否有默认方法可以去执行。
问题已经解决。

附解决方法:
1、在配置文件中加 'dispatcher_error' => "controller_empty();",
2、参考文档的自定义函数库,添加controller_empty()函数,函数内容如下:
function controller_empty() {
        GLOBAL $__controller, $__action;
        $action = $__action;
        // 对将要访问的控制器类进行实例化
        $handle_controller = spClass($__controller, null, $GLOBALS['G_SP']["controller_path"].'/'.$__controller.".php");
       
        if (is_object($handle_controller) && !method_exists($handle_controller, $__action) && method_exists($handle_controller, "_empty")){
                $action = "_empty";
        }else{
                $handle_controller->title = "错误404,没有找到的页面";
                $handle_controller->display("default/404.html");
                exit (0);
        }
       
        // 路由并执行用户代码
        $handle_controller->$action();
        // 控制器程序运行完毕,进行模板的自动输出
        if(FALSE != $GLOBALS['G_SP']['view']['auto_display']){
                $__tplname = $__controller.$GLOBALS['G_SP']['view']['auto_display_sep'].
                                $__action.$GLOBALS['G_SP']['view']['auto_display_suffix']; // 拼装模板路径
                $handle_controller->auto_display($__tplname);
        }
        // 对路由进行后续相关操作
        spLaunch("router_postfilter");
}
以后使用的时候只要在controller中添加_empty方法即可。在方法中使用GLOBAL $__action;来获取action名称。

2011-12-25 15:52:49

#4 jake

先看看手册吧,哎~~~

2011-12-25 16:10:12

#5 yanjingtao

先看看手册吧,哎~~~
jake 发表于 2011-12-25 16:10
刚看了。。。。呃。。。确实通过rewrite可以实现。。。。我汗。。。好吧。。。。主要是那段太长了。。。。而且,没感觉到那个和rewrite有关系。。。。。

2011-12-25 17:08:29

#6 景上青


        'default_controller' => 'main', // 默认的控制器名称
        'default_action' => 'index',  // 默认的动作名称
        'url_controller' => 'c',  // 请求时使用的控制器变量标识
        'url_action' => 'a',  // 请求时使用的动作变量标识

2011-12-25 20:38:44