开发文档 v2.x

响应(Response)

更新时间:2024年3月5日 10:07 浏览:588

响应对象 通过 Be::getResponse() 获取,主要用来向访问者输出数据。

status - 请求状态

public function status(int $code = 302, string $message = '')

参数:

  • $code - 状态码
  • $message - 状态信息

代码示例:

$response = Be::getResponse();

$response->status(404);
// 如果需要中止后续程序执行,记得要加 retrun
return;
// 其它业务代码 ...

 

redirect - 请求重定向

public function redirect(string $url, int $code = 302)

参数:

  • $url - 跳转网址
  • $code - 状态码

代码示例:

$response = Be::getResponse();

// 跳转到登录页
$response->redirect(beAdminUrl('System.AdminUser.login'));
// 如果需要中止后续程序执行,记得要加 retrun
return;
// 其它业务代码 ...

 

header - 输出头信息

public function header(string $key, string $val)

参数:

  • $key - 名称
  • $val - 值

 

write - 输出内容

public function write(string $string)

参数:

  • $string - 内容

代码示例:

$response = Be::getResponse();

$response->write('Hello world!');

不要使用 echo 直接输出数据,因为在 Swoole 模式下,echo 输出的内容,不会输出给用户,而是输出在了控制台

 

end - 输出内容并结束响应

public function end(string $string = '')

参数:

  • $string - 内容

普通php模式下,end 方法与 write 方法没有区别

Swoole 模式下,此方法输出内容后,会中止用户的 http 连接,用户页面加载完成。但后端程序仍可以继续执行后续的业务代码,

两种模式下最终的结果是一致的,不同点就是 Swoole 械式下用户很快就加载完成了页面。普通 php 模式下,用户需要等所有业务代码执行完才完成页面加载。

代码示例:

$response = Be::getResponse();

$response->end('Hello world!');

// 普通 php 械式下,需要等 10 秒后页面才加载完成
// Swoole 械式下,用户此时页面已加载完成,后续的操作仅在服务器执行,对用户无影响
sleep(10);

 

json - 以 JSON 输出暂存数据

public function json($data = null)

代码示例:

$response = Be::getResponse();

// 以 json 格式直接输出
$response->json([
    'success' => true,
    'code' => 100
    'message' => '提交成功!',
]);

// 暂存数据后,再输出
$response->set('success', true);
$response->set('code', 100);
$response->set('message','提交成功!');
$response->json();

 

cookie - 设置 Cookie

public function cookie(string $key, 
    string $value = '', 
    int $expire = 0, 
    string $path = '/', 
    string $domain = '', 
    bool $secure = false, 
    bool $httpOnly = false);

参数:

  • $key - 名称
  • $value - 值
  • $expire - 有效期(时间戳)
  • $path - 路径
  • $domain - 域名
  • $secure - 否通过安全的 HTTPS 连接来传输 cookie
  • $httpOnly - 是否仅 http 可用,开启后 js 将无权读取该 cookie

代码示例:

$response = Be::getResponse();

// 30天有效期
$response->cookie('remember_me', 1, time() + 30 * 86400);

// 指定目录
$response->cookie('remember_me', 1, time() + 30 * 86400, '/doc/');

// 指定域名
$response->cookie('remember_me', 1, time() + 30 * 86400, '/', 'www.phpbe.com');

// 仅允许 HTTPS 下使用
$response->cookie('remember_me', 1, time() + 30 * 86400, '/', '', true);

// 防止 JS 操作
$response->cookie('remember_me', 1, time() + 30 * 86400, '/', '', false, true);

 

display - 显示模板

public function display(string $templateName = null, string $themeName = null)

参数:

  • $templateName : 模板名
  • $themeName : 主题名

代码示例:

$response = Be::getResponse();

// 演染 /app/Cms/Template/Article/detail.php 模板文件
$response->display('App.Cms.Article.detail');

// 使用 Blank 主题演染 模板文件
$response->display('App.Cms.Article.detail', 'Blank');

// 自动演染当前请求的 "App.应用.控制器.动作"  命名的的模板文件
$response->display();

 

fetch - 获取模板渲染结果,但不发送给用户

public function fetch(string $templateName, string $themeName = null)

参数:

  • $templateName : 模板名
  • $themeName : 主题名

代码示例:

$response = Be::getResponse();

// 演染结果以字符串形式返回
$html = $response->display('App.Cms.Article.detail');

 

success - 成功页面

public function success(string $message, array $redirect = null)

参数:

  • $message : 消息
  • $redirect : 跳转

代码示例:

$response = Be::getResponse();

// 3秒后跳转
$redirect = [
    'timeout' => 3,
    'url' => beAdminUrl('System.Index.Index'),
]
$response->success('登录成功!', $redirect);


// 定制页面消息
$redirect = [
    'timeout' => 5,
    'url' => beAdminUrl('System.Index.Index'),
    'message' => '{timeout} 秒后跳转到 <a href="{url}">主页</a>',
]
$response->success('登录成功!', $redirect);

 

error - 出错页面

public function error(string $message, array $redirect = null)

参数:

  • $message : 消息
  • $redirect : 跳转

 

exception - 系统异常

public function exception(\Throwable $e)

参数:

  • $e : 异常

代码示例:

$response = Be::getResponse();

try {
    // ...
} catch (\Throable $t) {
    $response->exception($t);
}

 

getResponse - 获取原生 Response 对像

public function getResponse(): \Swoole\Http\Response 

仅适用 swoole 模式下

快捷访问

响应对象提供了快捷访问类名:Response,可直接通过静态方法访问。

代码示例:

<?php
use Be\Response;

Response::write('Hello world!');

Response::redirect(beAdminUrl('System.AdminUser.login'));

 

导航