isPost()"."/> isPost()".">

Home  >  Article  >  PHP Framework  >  How does thinkphp5 determine whether it is a post request?

How does thinkphp5 determine whether it is a post request?

WBOY
WBOYOriginal
2022-04-25 11:42:165167browse

在thinkphp5中,可以利用isPost()方法判断是否是post请求,该方法的作用就是判断当前是否为Post提交数据的,结果若为true则判断是post请求,若为false则不是post请求,语法为“$this->isPost()”。

How does thinkphp5 determine whether it is a post request?

本文操作环境:Windows10系统、ThinkPHP5版、Dell G3电脑。

thinkphp5怎么判断是否是post请求

ThinkPHP中使用isPost()方法来判断当前是否为Post提交数据的。

如果我在做一个添加用户的操作时,我们可以设置一个User/useradd.html作为模板。然后写一个UserAction.php。在UserAction.php中写一个userAdd方法,使用isPost()来判断是否状态,就可以把提交前和提交后写在一个Action里面了。

//用户添加
public function userAdd(){
    if($this->isPost()){
    //处理
    }
    else{
        $this->display('userAdd');
    }
}

How does thinkphp5 determine whether it is a post request?

之前使用3.2版本时,经常会使用到

if(IS_POST){
}else{
 
}

在thinkphp5.1中,废除了IS_POST。

thinkphp5.1中,我们可以这样用,

控制器中引入

use think\facade\Request;
public function index()
    {
        if(Request::isPost()){
        //这样判断
 
        } else {
            
        }
    }

推荐学习:《PHP视频教程

The above is the detailed content of How does thinkphp5 determine whether it is a post request?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn