路由响应

Testing Is Documentation

tests/Router/ResponseTest.php路由响应 - 图1

QueryPHP 路由响应封装了常用的响应,比如模板、JSON、文件下载、URL 重定向等。

使用容器 response 服务

  1. \App::make('response')->json($data = null, int $status = 200, array $headers = [], bool $json = false): \Leevel\Http\JsonResponse;

Uses

  1. <?php
  2. use Leevel\Http\JsonResponse;
  3. use Leevel\Http\RedirectResponse;
  4. use Leevel\Http\Request;
  5. use Leevel\Router\Redirect;
  6. use Leevel\Router\Response as RouterResponse;
  7. use Leevel\Router\Url;
  8. use Leevel\Router\View;
  9. use Leevel\View\Phpui;
  10. use SplFileInfo;
  11. use SplFileObject;
  12. use Symfony\Component\HttpFoundation\BinaryFileResponse;
  13. use Symfony\Component\HttpFoundation\Response;

基本使用

  1. public function testBaseUse(): void
  2. {
  3. $view = $this->makeView();
  4. $redirect = $this->makeRedirect();
  5. $factory = new RouterResponse($view, $redirect);
  6. $response = $factory->make('hello');
  7. $this->assertInstanceof(Response::class, $response);
  8. $this->assertInstanceof(Response::class, $response);
  9. $this->assertSame('hello', $response->getContent());
  10. $this->assertSame(200, $response->getStatusCode());
  11. $headers = $response->headers->all();
  12. unset($headers['date']);
  13. $this->assertSame(['cache-control' => ['no-cache, private']], $headers);
  14. }

make 返回一个响应

  1. public function testMake(): void
  2. {
  3. $view = $this->makeView();
  4. $redirect = $this->makeRedirect();
  5. $factory = new RouterResponse($view, $redirect);
  6. $response = $factory->make('foo.bar', 404, ['foo' => 'bar']);
  7. $this->assertInstanceof(Response::class, $response);
  8. $this->assertInstanceof(Response::class, $response);
  9. $this->assertSame('foo.bar', $response->getContent());
  10. $this->assertSame(404, $response->getStatusCode());
  11. $this->assertSame(['foo' => ['bar']], $this->getFilterHeaders($response->headers->all()));
  12. }

view 返回视图响应

fixture 定义

tests/Router/assert/view1.php

  1. hello view1 for <?php echo $foo; ?>.
  1. public function testView(): void
  2. {
  3. $view = $this->makeView();
  4. $redirect = $this->makeRedirect();
  5. $factory = new RouterResponse($view, $redirect);
  6. $response = $factory->view('view1', ['foo' => 'bar']);
  7. $this->assertInstanceof(Response::class, $response);
  8. $this->assertInstanceof(Response::class, $response);
  9. $this->assertSame('hello view1 for bar.', $response->getContent());
  10. $this->assertSame(200, $response->getStatusCode());
  11. $this->assertSame([], $this->getFilterHeaders($response->headers->all()));
  12. }

view 返回视图响应支持自定义后缀

fixture 定义

tests/Router/assert/view1.foo

  1. hello view1.foo for <?php echo $foo; ?>.
  1. public function testViewWithCustomExt(): void
  2. {
  3. $view = $this->makeView();
  4. $redirect = $this->makeRedirect();
  5. $factory = new RouterResponse($view, $redirect);
  6. $response = $factory->view('view1', ['foo' => 'bar new'], '.foo');
  7. $this->assertInstanceof(Response::class, $response);
  8. $this->assertInstanceof(Response::class, $response);
  9. $this->assertSame('hello view1.foo for bar new.', $response->getContent());
  10. $this->assertSame(200, $response->getStatusCode());
  11. $headers = $response->headers->all();
  12. $this->assertSame([], $this->getFilterHeaders($headers));
  13. }

view 返回视图成功消息

默认错误模板为 success

fixture 定义

tests/Router/assert/success.php

  1. success! message is <?php echo $message; ?>,url is <?php echo $url; ?>,time is <?php echo $time; ?>.
  1. public function testViewSuccess(): void
  2. {
  3. $view = $this->makeView();
  4. $redirect = $this->makeRedirect();
  5. $factory = new RouterResponse($view, $redirect);
  6. $response = $factory->viewSuccess('it is success.');
  7. $this->assertInstanceof(Response::class, $response);
  8. $this->assertInstanceof(Response::class, $response);
  9. $this->assertSame('success! message is it is success.,url is ,time is 1.', $response->getContent());
  10. $this->assertSame(200, $response->getStatusCode());
  11. $this->assertSame([], $this->getFilterHeaders($response->headers->all()));
  12. }

setViewSuccessTemplate 返回视图成功消息支持设置视图正确模板

fixture 定义

tests/Router/assert/success_custom.php

  1. success custom! message is <?php echo $message; ?>,url is <?php echo $url; ?>,time is <?php echo $time; ?>.
  1. public function testViewSuccess3(): void
  2. {
  3. $view = $this->makeView();
  4. $redirect = $this->makeRedirect();
  5. $factory = new RouterResponse($view, $redirect);
  6. $factory->setViewSuccessTemplate('success_custom');
  7. $response = $factory->viewSuccess('it is success3.', 'http://queryphp.com', 3);
  8. $this->assertInstanceof(Response::class, $response);
  9. $this->assertInstanceof(Response::class, $response);
  10. $this->assertSame('success custom! message is it is success3.,url is http://queryphp.com,time is 3.', $response->getContent());
  11. $this->assertSame(200, $response->getStatusCode());
  12. $this->assertSame([], $this->getFilterHeaders($response->headers->all()));
  13. }

view 返回视图失败消息

默认错误模板为 fail

fixture 定义

tests/Router/assert/fail.php

  1. fail! message is <?php echo $message; ?>,url is <?php echo $url; ?>,time is <?php echo $time; ?>.
  1. public function testViewFail(): void
  2. {
  3. $view = $this->makeView();
  4. $redirect = $this->makeRedirect();
  5. $factory = new RouterResponse($view, $redirect);
  6. $response = $factory->viewFail('it is fail.');
  7. $this->assertInstanceof(Response::class, $response);
  8. $this->assertInstanceof(Response::class, $response);
  9. $this->assertSame('fail! message is it is fail.,url is ,time is 3.', $response->getContent());
  10. $this->assertSame(404, $response->getStatusCode());
  11. $this->assertSame([], $this->getFilterHeaders($response->headers->all()));
  12. }

setViewFailTemplate 返回视图失败消息支持设置视图错误模板

fixture 定义

tests/Router/assert/fail_custom.php

  1. fail custom! message is <?php echo $message; ?>,url is <?php echo $url; ?>,time is <?php echo $time; ?>.
  1. public function testViewFail3(): void
  2. {
  3. $view = $this->makeView();
  4. $redirect = $this->makeRedirect();
  5. $factory = new RouterResponse($view, $redirect);
  6. $factory->setViewFailTemplate('fail_custom');
  7. $response = $factory->viewFail('it is fail3.', 'http://queryphp.com', 3);
  8. $this->assertInstanceof(Response::class, $response);
  9. $this->assertInstanceof(Response::class, $response);
  10. $this->assertSame('fail custom! message is it is fail3.,url is http://queryphp.com,time is 3.', $response->getContent());
  11. $this->assertSame(404, $response->getStatusCode());
  12. $this->assertSame([], $this->getFilterHeaders($response->headers->all()));
  13. }

json 返回 JSON 响应

  1. public function testJson(): void
  2. {
  3. $view = $this->makeView();
  4. $redirect = $this->makeRedirect();
  5. $factory = new RouterResponse($view, $redirect);
  6. $response = $factory->json();
  7. $this->assertInstanceof(Response::class, $response);
  8. $this->assertInstanceof(Response::class, $response);
  9. $this->assertInstanceof(JsonResponse::class, $response);
  10. $this->assertSame('{}', $response->getContent());
  11. $this->assertSame(200, $response->getStatusCode());
  12. $this->assertSame(['content-type' => ['application/json']], $this->getFilterHeaders($response->headers->all()));
  13. }

json 返回 JSON 响应支持数组

  1. public function testJson3(): void
  2. {
  3. $view = $this->makeView();
  4. $redirect = $this->makeRedirect();
  5. $factory = new RouterResponse($view, $redirect);
  6. $response = $factory->json(['foo' => 'bar', 'hello' => 'world'], 404, ['foo' => 'bar']);
  7. $this->assertInstanceof(Response::class, $response);
  8. $this->assertInstanceof(Response::class, $response);
  9. $this->assertInstanceof(JsonResponse::class, $response);
  10. $this->assertSame('{"foo":"bar","hello":"world"}', $response->getContent());
  11. $this->assertSame(404, $response->getStatusCode());
  12. $this->assertSame(['foo' => ['bar'], 'content-type' => ['application/json']], $this->getFilterHeaders($response->headers->all()));
  13. }

json 返回 JSON 响应支持原生 JSON 字符串

  1. public function testJson4(): void
  2. {
  3. $view = $this->makeView();
  4. $redirect = $this->makeRedirect();
  5. $factory = new RouterResponse($view, $redirect);
  6. $response = $factory->json('{"foo":"bar","hello":"world"}', 404, ['foo' => 'bar'], true);
  7. $this->assertInstanceof(Response::class, $response);
  8. $this->assertInstanceof(Response::class, $response);
  9. $this->assertInstanceof(JsonResponse::class, $response);
  10. $this->assertSame('{"foo":"bar","hello":"world"}', $response->getContent());
  11. $this->assertSame(404, $response->getStatusCode());
  12. $this->assertSame(['foo' => ['bar'], 'content-type' => ['application/json']], $this->getFilterHeaders($response->headers->all()));
  13. }

jsonp 返回 JSONP 响应

  1. public function testJsonp(): void
  2. {
  3. $view = $this->makeView();
  4. $redirect = $this->makeRedirect();
  5. $factory = new RouterResponse($view, $redirect);
  6. $response = $factory->jsonp('foo');
  7. $this->assertInstanceof(Response::class, $response);
  8. $this->assertInstanceof(Response::class, $response);
  9. $this->assertInstanceof(JsonResponse::class, $response);
  10. $this->assertSame('/**/foo({});', $response->getContent());
  11. $this->assertSame(200, $response->getStatusCode());
  12. $this->assertSame(['content-type' => ['text/javascript']], $this->getFilterHeaders($response->headers->all()));
  13. }

download 返回下载响应

  1. public function testDownload(): void
  2. {
  3. $view = $this->makeView();
  4. $redirect = $this->makeRedirect();
  5. $factory = new RouterResponse($view, $redirect);
  6. $response = $factory->download(__DIR__.'/assert/download.txt');
  7. $this->assertInstanceof(Response::class, $response);
  8. $this->assertInstanceof(Response::class, $response);
  9. $this->assertInstanceof(BinaryFileResponse::class, $response);
  10. $this->assertFalse($response->getContent());
  11. $this->assertSame(200, $response->getStatusCode());
  12. $this->assertSame('attachment; filename=download.txt', $response->headers->all()['content-disposition'][0]);
  13. }

download 返回下载响应支持 \SplFileInfo

  1. public function testDownload2(): void
  2. {
  3. $view = $this->makeView();
  4. $redirect = $this->makeRedirect();
  5. $factory = new RouterResponse($view, $redirect);
  6. $response = $factory->download(new SplFileInfo(__DIR__.'/assert/download.txt'));
  7. $this->assertInstanceof(Response::class, $response);
  8. $this->assertInstanceof(Response::class, $response);
  9. $this->assertInstanceof(BinaryFileResponse::class, $response);
  10. $this->assertFalse($response->getContent());
  11. $this->assertSame(200, $response->getStatusCode());
  12. $this->assertSame('attachment; filename=download.txt', $response->headers->all()['content-disposition'][0]);
  13. }

download 返回下载响应支持 \SplFileObject

  1. public function testDownload3(): void
  2. {
  3. $view = $this->makeView();
  4. $redirect = $this->makeRedirect();
  5. $factory = new RouterResponse($view, $redirect);
  6. $response = $factory->download(new SplFileObject(__DIR__.'/assert/download.txt'));
  7. $this->assertInstanceof(Response::class, $response);
  8. $this->assertInstanceof(Response::class, $response);
  9. $this->assertInstanceof(BinaryFileResponse::class, $response);
  10. $this->assertFalse($response->getContent());
  11. $this->assertSame(200, $response->getStatusCode());
  12. $this->assertSame('attachment; filename=download.txt', $response->headers->all()['content-disposition'][0]);
  13. }

download 返回下载响应支持自定义文件名

  1. public function testDownload4(): void
  2. {
  3. $view = $this->makeView();
  4. $redirect = $this->makeRedirect();
  5. $factory = new RouterResponse($view, $redirect);
  6. $response = $factory->download(__DIR__.'/assert/download.txt', 'foo.txt', 200, ['foo' => 'bar']);
  7. $this->assertInstanceof(Response::class, $response);
  8. $this->assertInstanceof(Response::class, $response);
  9. $this->assertInstanceof(BinaryFileResponse::class, $response);
  10. $this->assertFalse($response->getContent());
  11. $this->assertSame(200, $response->getStatusCode());
  12. $this->assertSame('attachment; filename=foo.txt', $response->headers->all()['content-disposition'][0]);
  13. $this->assertSame('bar', $response->headers->all()['foo'][0]);
  14. }

file 返回文件响应

  1. public function testFile(): void
  2. {
  3. $view = $this->makeView();
  4. $redirect = $this->makeRedirect();
  5. $factory = new RouterResponse($view, $redirect);
  6. $response = $factory->file(__DIR__.'/assert/download.txt');
  7. $this->assertInstanceof(Response::class, $response);
  8. $this->assertInstanceof(Response::class, $response);
  9. $this->assertInstanceof(BinaryFileResponse::class, $response);
  10. $this->assertFalse($response->getContent());
  11. $this->assertSame(200, $response->getStatusCode());
  12. $this->assertSame('inline; filename=download.txt', $response->headers->all()['content-disposition'][0]);
  13. }

file 返回文件响应支持 \SplFileInfo

  1. public function testFile2(): void
  2. {
  3. $view = $this->makeView();
  4. $redirect = $this->makeRedirect();
  5. $factory = new RouterResponse($view, $redirect);
  6. $response = $factory->file(new SplFileInfo(__DIR__.'/assert/download.txt'));
  7. $this->assertInstanceof(Response::class, $response);
  8. $this->assertInstanceof(Response::class, $response);
  9. $this->assertInstanceof(BinaryFileResponse::class, $response);
  10. $this->assertFalse($response->getContent());
  11. $this->assertSame(200, $response->getStatusCode());
  12. $this->assertSame('inline; filename=download.txt', $response->headers->all()['content-disposition'][0]);
  13. }

file 返回文件响应支持 \SplFileObject

  1. public function testFile3(): void
  2. {
  3. $view = $this->makeView();
  4. $redirect = $this->makeRedirect();
  5. $factory = new RouterResponse($view, $redirect);
  6. $response = $factory->file(new SplFileObject(__DIR__.'/assert/download.txt'));
  7. $this->assertInstanceof(Response::class, $response);
  8. $this->assertInstanceof(Response::class, $response);
  9. $this->assertInstanceof(BinaryFileResponse::class, $response);
  10. $this->assertFalse($response->getContent());
  11. $this->assertSame(200, $response->getStatusCode());
  12. $this->assertSame('inline; filename=download.txt', $response->headers->all()['content-disposition'][0]);
  13. }

redirect 返回一个 URL 生成跳转响应

  1. public function testRedirect(): void
  2. {
  3. $view = $this->makeView();
  4. $redirect = $this->makeRedirect();
  5. $factory = new RouterResponse($view, $redirect);
  6. $response = $factory->redirect('hello/world');
  7. $content = <<<'eot'
  8. <!DOCTYPE html>
  9. <html>
  10. <head>
  11. <meta charset="UTF-8" />
  12. <meta http-equiv="refresh" content="0;url='http://www.queryphp.com/hello/world'" />
  13. <title>Redirecting to http://www.queryphp.com/hello/world</title>
  14. </head>
  15. <body>
  16. Redirecting to <a href="http://www.queryphp.com/hello/world">http://www.queryphp.com/hello/world</a>.
  17. </body>
  18. </html>
  19. eot;
  20. $this->assertInstanceof(Response::class, $response);
  21. $this->assertInstanceof(Response::class, $response);
  22. $this->assertInstanceof(RedirectResponse::class, $response);
  23. $this->assertSame($content, $response->getContent());
  24. $this->assertSame(302, $response->getStatusCode());
  25. $this->assertSame(['location' => ['http://www.queryphp.com/hello/world']], $this->getFilterHeaders($response->headers->all()));
  26. }

redirectRaw 返回一个跳转响应

  1. public function testRedirectRaw(): void
  2. {
  3. $view = $this->makeView();
  4. $redirect = $this->makeRedirect();
  5. $factory = new RouterResponse($view, $redirect);
  6. $response = $factory->redirectRaw('http://queryphp.com/raw');
  7. $content = <<<'eot'
  8. <!DOCTYPE html>
  9. <html>
  10. <head>
  11. <meta charset="UTF-8" />
  12. <meta http-equiv="refresh" content="0;url='http://queryphp.com/raw'" />
  13. <title>Redirecting to http://queryphp.com/raw</title>
  14. </head>
  15. <body>
  16. Redirecting to <a href="http://queryphp.com/raw">http://queryphp.com/raw</a>.
  17. </body>
  18. </html>
  19. eot;
  20. $this->assertInstanceof(Response::class, $response);
  21. $this->assertInstanceof(Response::class, $response);
  22. $this->assertInstanceof(RedirectResponse::class, $response);
  23. $this->assertSame($content, $response->getContent());
  24. $this->assertSame(302, $response->getStatusCode());
  25. $this->assertSame(['location' => ['http://queryphp.com/raw']], $this->getFilterHeaders($response->headers->all()));
  26. }