响应是由 think\Response 类或者子类完成
响应输出
<?php
namespace app\index\controller;
use think\exception\ValidateException;
use think\facade\Request;
class Index extends BaseController
{
/**
* 显示资源列表
*
* @return \think\Response
*/
public function index()
{
$data = 'ming';
return response($data);
}
/**
* 显示创建资源表单页.
*
* @return \think\Response
*/
public function create()
{
//
}
/**
* 保存新建的资源
*
* @param \think\Request $request
* @return \think\Response
*/
public function save(Request $request)
{
//
}
/**
* 显示指定的资源
*
* @param int $id
* @return \think\Response
*/
public function read($id)
{
//
}
/**
* 显示编辑资源表单页.
*
* @param int $id
* @return \think\Response
*/
public function edit($id)
{
//
}
/**
* 保存更新的资源
*
* @param \think\Request $request
* @param int $id
* @return \think\Response
*/
public function update(Request $request, $id)
{
//
}
/**
* 删除指定资源
*
* @param int $id
* @return \think\Response
*/
public function delete($id)
{
//
}
public function __call($name, $arguments)
{
// TODO: Implement __call() method.
return 'error request';
}
}
<?php
namespace app\index\controller;
use think\exception\ValidateException;
use think\facade\Request;
class Index extends BaseController
{
/**
* 显示资源列表
*
* @return \think\Response
*/
public function index()
{
$data = 'ming';
return response($data);
}
/**
* 显示创建资源表单页.
*
* @return \think\Response
*/
public function create()
{
//
}
/**
* 保存新建的资源
*
* @param \think\Request $request
* @return \think\Response
*/
public function save(Request $request)
{
//
}
/**
* 显示指定的资源
*
* @param int $id
* @return \think\Response
*/
public function read($id)
{
//
}
/**
* 显示编辑资源表单页.
*
* @param int $id
* @return \think\Response
*/
public function edit($id)
{
//
}
/**
* 保存更新的资源
*
* @param \think\Request $request
* @param int $id
* @return \think\Response
*/
public function update(Request $request, $id)
{
//
}
/**
* 删除指定资源
*
* @param int $id
* @return \think\Response
*/
public function delete($id)
{
//
}
public function __call($name, $arguments)
{
// TODO: Implement __call() method.
return 'error request';
}
}
响应参数
链式处理
重定向
使用redirect助手函数重定向
访问 http://localhost:8082/index.php/index/index/ 重定向到 http://localhost:8082/index.php/index/index/hello.html
/**
* 显示资源列表
*
* @return \think\Response
*/
public function index()
{
return redirect('hello')->with('name', 'thinkphp');
}
public function hello(){
$name = session('name');
return 'hello' . $name;
}
传参使用params传入
文件下载
public function download()
{
$data = '这是一个测试文件';
return download($data, 'test.txt', true);
}