关闭路由后的普通模式任然可以通过操作方法的参数绑定、控制器和空操作等特性实现url地址的简化

参数绑定(默认是按名称成对解析,):

namespace app\index\Controller;

class Blog
{
public function read($id)
{
return 'id='.$id;
} public function archive($year='2016',$month='01')
{
return 'year='.$year.'&month='.$month;
}
}

//上例对应的URL访问地址分别是

http://serverName/index.php/index/blog/read/id/5

http://serverName/index.php/index/blog/archive/year/2016/month/06


输出结果:
id=5
year=2016&month=06

按照顺序解析变量需要修改配置文件的url_param_type参数

// 按照顺序解析变量
'url_param_type' => 1,

上面的例子修改下访问url地址

//修改url中year和month参数值的顺序
http://serverName/index.php/index/blog/archive/06/2016 输出结果:
year=06&month=2016

按顺序绑定参数,操作方法的参数只能使用URL pathinfo变量,而不能使用get或者post变量

参数绑定有一个特例,操作方法中定义有Request对象作为参数,无论参数位置在哪里,都会自动注入,而不需要进行参数绑定

namespace app\index\Controller;
use think\Request class Blog
{
public function demo1()
{
$year=Request:instance()->param('year');
$month=Request:instance()->param('month');
$all=Request:instance()->param();//获取全部参数变量
$get=Request:instance()->get();//获取url?后的参数变量(获取到year变量2018)
$rt=Request:instance()->route();//获取路径后面的参数变量(只获取到id变量123)
$post=Request:instance()->post();//获取post参数变量(只获取到age变量18) } public function demo2(){
//input获取url变量 同tp3的I()
$id=input('get.id') } public function demo3(Request $request){
//依赖注入
$all=$request->param(); }
} http://localhost/demo1/123?year=2018(month变量为post传递)

架构方法(构造方法)参数绑定(V5.0.1)

当前请求的路由变量可以自动绑定到析构函数的参数,

namespace app\index\Controller;

class Blog
{
protected $name;
public function __construct($name = null)
{
$this->name = $name;
}
}
//如果访问http://localhost/index/index/index/name/thinkphp
//当前请求路由变量name,则thinkphp会自动传入析构方法里的name变量

tp5参数绑定的更多相关文章

  1. Spring MVC初始化参数绑定

    初始化参数绑定与类型转换很类似,初始化绑定时,主要是参数类型 ---单日期 在处理器类中配置绑定方法  使用@InitBinder注解 在这里首先注册一个用户编辑器 参数一为目标类型   proper ...

  2. SpringMVC初始化参数绑定--日期格式

    一.初始化参数绑定[一种日期格式] 配置步骤: ①:在applicationcontext.xml中只需要配置一个包扫描器即可 <!-- 包扫描器 --> <context:comp ...

  3. SpringMvc中初始化参数绑定

    初始化参数绑定与类型转换很类似,初始化绑定时,主要是参数类型 ---单日期 在处理器类中配置绑定方法  使用@InitBinder注解 在这里首先注册一个用户编辑器 参数一为目标类型   proper ...

  4. @RequestParam @RequestBody @PathVariable 等参数绑定注解详解

    文章主要讲解request 数据到handler method 参数数据的绑定所用到的注解和什么情形下使用. 简介: handler method 参数绑定常用的注解,我们根据他们处理的Request ...

  5. spring 参数绑定

    部分资料来源: @RequestParam @RequestBody @PathVariable 等参数绑定注解详解 spring学习之@ModelAttribute运用详解 Spring MVC @ ...

  6. springmvc的初始化参数绑定

    一.springmvc的初始化参数绑定 此种和我们之前说的类型转换非常相似,可以看作是一种类型转换 在初始化参数绑定时  重要的是参数类型 -------------------单日期的绑定 二. 配 ...

  7. MVC中Action参数绑定的过程

    一.题外话 上一篇:MVC中Action的执行过程 ControllerContext 封装有了与指定的 RouteBase 和 ControllerBase 实例匹配的 HTTP 请求的信息. 二. ...

  8. springmvc参数绑定

    1. @PathVariable当使用@RequestMapping URI template 样式映射时, 即 someUrl/{paramId}, 这时的paramId可通过 @Pathvaria ...

  9. spring mvc参数绑定

    spring绑定参数的过程 从客户端请求key/value数据,经过参数绑定,将key/value数据绑定到controller方法的形参上.springmvc中,接收页面提交的数据是通过方法形参来接 ...

随机推荐

  1. Linux文件系统深度讨论【转】

      本文旨在对Linux文件系统概念高级工作方式进行的讨论,不是对特定文件系统类型(如EXT4)如何工作的低级描述,也不是对文件系统命令的教程. 每台通用计算机都需要将各种类型的数据存储在硬盘驱动器( ...

  2. python numpy中数组.min()

    import numpy as np a = np.array([[1,5,3],[4,2,6]]) print(a.min()) #无参,所有中的最小值 print(a.min(0)) # axis ...

  3. swift 实践- 03 -- UILabel

    class ViewController: UIViewController { override func viewDidLoad() { super.viewDidLoad() // 标签 let ...

  4. chkconfig: command not found

    问题描述 Ubuntu 16.04 下安装 Nginx 服务器,在添加 nginx 服务时出现如下信息 # chkconfig --add nginx chkconfig: command not f ...

  5. -Dmaven.multiModuleProjectDirectory system property is not set.

    1.配置 maven 环境变量 新建系统变量 -> 变量名(N): M2_HOME 变量值(V): D:\apache-maven-3.5.4(改为自己的maven目录) -> 添加 pa ...

  6. Confluence 6 安装 SQL Server

    如果你还没有在安装可以连接的 Microsoft SQL Server 数据库,请先下载后进行安装.请参考 MSDN 上 Installation for SQL Server 的指南.  有关授权模 ...

  7. 【Web】servlet、filter和listener

    一般地,servlet.filter.listener是配置到web.xml中(web.xml 的加载顺序是:context-param -> listener -> filter -&g ...

  8. 【Linux】添加DNS

    1.添加DNS输入命令: vi /etc/resolv.conf 添加一行:nameserver 10.41.132.9 2.查看DNS nslookup 127.0.0.1 | grep Serve ...

  9. Scratch 2.0-Find The Mouse 发布!

    日期:2018.8.26 星期日 博客期:007 今天随便写了一个小型游戏程序,哈哈!虽然小,但用到的逻辑还是有一定水平的.呼~毕竟就这一下子也写不出来微软一样的公司嘛!哈哈,截图放上来! 游戏分为四 ...

  10. 【ES】学习9-聚合2

    按时间统计:date_histogram GET /cars/transactions/_search { , "aggs": { "sales": { &qu ...