美文网首页
thinkphp5 路由总结 从配置到模版页面访问

thinkphp5 路由总结 从配置到模版页面访问

作者: geeooooz | 来源:发表于2017-12-28 15:10 被阅读149次

1.开启路由配置

首先,我使用的是混合模式 关于模式请点击 https://www.kancloud.cn/manual/thinkphp5/118019

'url_route_on' => true, //开启路由

// 按照顺序解析变量
'url_param_type'    =>  1,
'url_route_must' => true,//表示强制开启 必须定义路由才能访问 一般都是为false

2.路由具体配置

首先,这是 index模块儿/Index控制器/hello方法

<?php
namespace app\index\controller;
use think\Controller;
class Index extends Controller
{
  public function hello($name = 'World')
  {
    return 'Hello,' . $name . '!';
  }
}

那么我们在application\route.php 中写入路由配置:
1.:name为传递的参数 必传 否侧报错

return [
        // 添加路由规则 路由到 index控制器的hello操作方法
        'hello/:name' => 'index/index/hello',

现在我们访问http://auth.com/hello/world 出现以下场景 是正确的


那么我们访问 http://auth.com/hello/呢? 报错

2.:name为选填参数 不是必传 []代表不是必传

return [
        // 路由参数name为可选
        'hello/[:name]' => 'index/index/hello',

现在再访问http://auth.com/hello 成功

3.【完整匹配】

    // 路由参数name为可选
        'hello/[:name]$' => 'index/hello',
//      当路由规则以$结尾的时候就表示当前路由规则需要完整匹配。
//      当我们访问下面的URL地址的时候:
//      http://auth.com/hello // 正确匹配
//      http://auth.com/hello/thinkphp // 正确匹配
//      http://auth.com/hello/thinkphp/val/value // 不会匹配

4.【路由参数】

return [
  // 定义路由的请求类型和后缀
  'hello/[:name]' => ['index/hello', ['method' => 'get', 'ext' => 'html']],
];

上面定义的路由规则限制了必须是get请求,而且后缀必须是html的,所以下面的访问地址:

http://auth.com/hello// 无效
http://auth.com/hello.html// 有效
http://auth.com/hello/thinkphp// 无效
http://auth.com/hello/thinkphp.html// 有效

现在重新在index模块下创建Blog控制器:

<?php
namespace app\index\controller;
class Blog
{
  public function get($id)
  {
    return '查看id=' . $id . '的内容';
  }
  public function read($name)
  {
    return '查看name=' . $name . '的内容';
  }
  public function archive($year, $month)
  {
    return '查看' . $year . '/' . $month . '的归档内容';
  }
}

现在就用到路由分组了

//【路由分组】
return [    
  '[blog]' => [
    ':year/:month' => ['index/blog/archive', ['method' => 'get'], ['year' => '\d{4}', 'month' => '\d{2}']], 
    ':id'     => ['index/blog/get', ['method' => 'get'], ['id' => '\d+']],
    ':name'    => ['index/blog/read', ['method' => 'get'], ['name' => '\w+']],
  ],
];

关于更多的route下 路由配置可以去tp5官方文档查看https://www.kancloud.cn/manual/thinkphp5/118029
也可以看看这篇文章 http://www.php.cn/php-weizijiaocheng-383385.html

3.如何在模版文件中跳转正确的路由

<a href="{:url('index/blog/get', 'id=1')}"><p>Go</p></a>

index/blog/get 及 id=1 对应


大致就是如此 其他的也都差不多

TP5里面是这么写的


到这里就大功告成了 大家可以根据需求进行详细配置

相关文章

  • thinkphp5 路由总结 从配置到模版页面访问

    1.开启路由配置 首先,我使用的是混合模式 关于模式请点击 https://www.kancloud.cn/man...

  • Nuxt.js基础

    Nuxt.js工作流 Nuxt.js目录 路由&示例 新建好即等于配置好 页面模版&示例 模版是layouts 异...

  • vue重构有赞商城- 路由配置

    一、路由配置一 1. 创建router模版 html:(个人中心页面member.html) js:(member...

  • Vue-router配置子路由

    子路由 子菜单的路由方式,也叫子路由。子路由一般用在一个页面有他的基础模版,然后它下面的页面都隶属于这个模版,只是...

  • ThinkPHP5路由

    ThinkPHP5路由 1 PathInfo方式的路由平常我们按照习惯去访问我们的ThinkPHP项目的时候,常规...

  • 路由权限配置(页面访问控制)

    当我们不想给某些没有权限的用户访问某些页面的时候, 我们可以让他访问的时候,跳转到别的页面去。

  • 微信小程序基础

    app.json配置 管理页面路由,窗口设置,tabBar配置 app.js中全局数据的访问 定义 在page中获...

  • 配置虚拟主机 xampp

    软件:xampp框架:thinkphp5 配置虚拟主机 路由格式 入口文件/模块/controller/actio...

  • VUE3(六)项目配置使用404页面

    当我们访问的路由没有预先定义的时候,就会跳到一个空白的页面。 这样的体验不太好,那么我们需要配置,访问路由不存在时...

  • cookie的应用

    需求 当访问 / 路由的时候,把我所有的搜索的东西显示到对应的页面上 当访问/search路由的时候,记录对应的搜...

网友评论

      本文标题:thinkphp5 路由总结 从配置到模版页面访问

      本文链接:https://www.haomeiwen.com/subject/jqfvgxtx.html