2.2. 分析程序

我们首先定义了一个HelloWorldController类,含有一个简单的方法 appIndex。

访问时到底发生了什么?

  1. 浏览器请求 /helloWorld,I-F自动识别这个地址和HelloWorldController类相对应

  2. I-F自动实例化HelloWorldController,并调用它的一个方法appIndex

  3. 于是激活了echo "hello,world"

现在我们添加更多的appXxx方法:

<?php

class HelloWorldController extends IApplication {
    public function 
appIndex() {
        echo 
"Hello,World";
    }
    
    public function 
appTest1() {
        echo 
"test1";
    }
    
    public function 
appTest2() {
        echo 
"test2";
    }
}

?>

这时候分别请求http://localhost/test/index.php/helloWorld/test1和http://localhost/test/index.php/helloWorld/test2,结果发现分别打印除了test1和test2。

发生了什么?

  1. 浏览器请求 /helloWorld,I-F自动识别这个地址和HelloWorldController类相对应

  2. I-F自动实例化HelloWorldController,并调用它的一个方法appTest1(或appTest2)

  3. 于是打印除了test1(或test2)

由此我们知道,其实我们的HelloWorldController类的方法是和用户输入的请求一一对应的,对于action=xxx,对应的要调用的方法就是appXxx,另外

http://localhost/test/index.php/helloWorld/index
http://localhost/test/index.php/helloWorld

的效果是一样的,因为 index是一个特殊的action,当没有传入任何action时,就调用index。

这里的HelloWorldController类其实是MVC中的Controller,我们将在后面讲述的更多相关的内容。