IT博客汇
  • 首页
  • 精华
  • 技术
  • 设计
  • 资讯
  • 扯淡
  • 权利声明
  • 登录 注册

    Laravel 5.5 为响应请求提供的可响应接口

    小李刀刀发表于 2017-10-31 05:52:35
    love 0
    Laravel 5.5 的路由中增加了一种新的返回类型:可相应接口(Responsable)。该接口允许对象在从控制器或者闭包路由中返回时自动被转化为标准的 HTTP 响应接口。任何实现 Responsable 接口的对象必须实现一个名为 toResponse() 的方法,该方法将对象转化为 HTTP 响应对象。看示例: use Illuminate\Contracts\Support\Responsable; class ExampleObject implements Responsable { public function __construct($name = null) { $this->name = $name ?? 'Teapot'; } public function status() { switch(strtolower($this->name)) { case 'teapot': return 418; default: return 200; } } public function toResponse() { return response( Hello {$this->name}, $this->status(), ['X-Person' […]


沪ICP备19023445号-2号
友情链接