04. URL 访问模式

【十天精品课堂系列】 主讲:李炎恢

学习要点:

1.URL 解析
2.URL 兼容模式
本节课我们来了解一下 ThinkPHP6.0 的 URL 访问模式,解析它的访问方法。

一.URL 解析

  1. ThinkPHP 框架非常多的操作都是通过 URL 来实现的;
  2. 多应用:http://serverName/index.php/应用/控制器/操作/参数/值…;
  3. 单应用:http://serverName/index.php/控制器/操作/参数/值…;
  4. 由于 TP6.0 默认是单应用模式,多应用需要作为扩展安装,避免混乱暂时搁置;
  5. http://serverName 是域名地址,比如 127.0.0.1:8000 或 localhost/tp6/
  6. index.php 这个文件,是根目录下 public/下的 index.php(入口文件);
  7. 控制器:app 目录下有一个 controller 控制器目录的 Test.php(控制器);
  8. Test.php 控制器的类名也必须是 class Test,否则错误;
  9. 操作就是控制器类里面的方法,比如:index(默认免写)或 hello(必写);
  10. 那么完整形式为:

a. http://localhost/tp6/public/index.php/test/hello/value/world
b. http://127.0.0.1:8000/index.php/test/hello/value/world

class Test
{
public function index()
{
return 'test';
}
public function hello($value = '')
{
return 'hello '.$value;
}
}
  1. public/index.php 中的 index.php 可以省略,只要设置 URL 重写即可;
  2. httpd.conf 配置文件中加载了 mod_rewrite.so 模块;
  3. AllowOverride None 将 None 改为 All;
  4. 此时,路径变更为:

http://localhost/tp6/public/test/hello/value/world

二.URL 兼容模式

  1. 上个要点已经了解了 URL 所有访问规则,通过创建 Test 控制器更加了解;
  2. 如果上面那种形式的 URL 不支持的话,可以使用兼容模式的方式来访问:

http://localhost/tp6test3/public/?s=test/hello/value/world

你可能感兴趣的:(04. URL 访问模式)