Redirect Response

Testing Is Documentation

tests/Http/RedirectResponseTest.phpRedirect Response - 图1

QueryPHP 针对页面重定向可以直接返回一个 \Leevel\Http\RedirectResponse 响应对象。

Uses

  1. <?php
  2. use Leevel\Http\RedirectResponse;
  3. use Leevel\Session\ISession;

with 闪存一个数据片段到 SESSION

  1. public function testWith(): void
  2. {
  3. $response = new RedirectResponse('foo.bar');
  4. $response->setSession($this->mokeSessionForWith());
  5. $this->assertInstanceOf(ISession::class, $response->getSession());
  6. $response->with('foo', 'bar');
  7. $this->assertSame($response->getSession()->getFlash('foo'), 'bar');
  8. }

withErrors 闪存错误信息

  1. public function testWithError(): void
  2. {
  3. $errorsDefault = [
  4. 'name' => 'less than 6',
  5. 'age' => 'must be 18',
  6. ];
  7. $errorsCustom = [
  8. 'foo' => 'bar is error',
  9. ];
  10. $data = ['default' => $errorsDefault, 'custom' => $errorsCustom];
  11. $response = new RedirectResponse('foo.bar');
  12. $response->setSession($this->mokeSessionForWithError($data));
  13. $this->assertInstanceOf(ISession::class, $response->getSession());
  14. $response->withErrors($errorsDefault);
  15. $response->withErrors($errorsCustom, 'custom');
  16. $this->assertSame($response->getSession()->getFlash('errors'), $data);
  17. }