ThinkPHP5.0---------请求和响应

1.请求

①使用助手函数,$request =request();

②use  think\Request;

$request=Request::instance();

③在参数中注入对象(建议用这个)

use think\Request

例如  function index(Request $request)

//http://xin.com/admin/index/res/type/4.html?id=1  

dump($request->domain());//域名  

dump($request->pathinfo());//整个路径(除了查询字符串)admin/index/res/type/4.html  

dump($request->path());//不含后缀admin/index/res/type/4  

dump($request->method());  

dump($request->isGet()); //isPost()  isAjax();  

dump($request->get());//获取查询字符串的值  

dump($request->param());//index/index/index/type/5.html?id=2  获取type=>5 和id=>2  

// session('name','huangyuxin');  

dump($request->session());  

dump($request->session('name'));   

// cookie('email','[email protected]');  

dump($request->cookie());  

dump($request->module());  

dump($request->controller());  

dump($request->action());//方法  

dump($request->url());//获取完整路径  

dump($request->baseUrl());//'/admin/index/res/type/4.html'  

dump($request->get('id','haha','intval'));第三个参数是强制类型转换

2.input助手函数

    我们会看到它首先有一个判断,判断当前系统是否存在input函数,如果存在,它就会跳过该部分,不再定义input函数。我们知道在php中,如果我们将一个函数重新定义,那么程序是会报错的,所以我们使用input函数是有一定风险的。我们在执行框架之前,也就是说我们在入口文件的地方,如果我们引入其他的文件,我们在其他文件中定义了input这个函数,那么系统将无法使用input这个函数来获取我们的各种参数,它还是使用我们自定义的那个input值。当然,在我们使用框架的时候,我们规范中是不允许重新定义的,或者说,我们团队中应该有这样的规范,不能定义和我们系统助手函数相同的函数名,如果定义的话,如果在之前引入,那系统中的函数将不能使用,如果在之后使用,那么程序会报错。

1.  echo input('session.emails','default'); //要是emails不存在的话,返回default  

2. dump(input('id'));    

3.  dump(input('get.id'));  

3.response

thinkphp为我们提供了响应对象,我们通过调整一些简单的参数,来让我们的response或者来让我们的程序返回不同的结果。

use think\Config;

Config::set('default_return_type','json');

 

 

你可能感兴趣的:(ThinkPHP)