response() helper nếu không có tham số thì trả về Illuminate\Contracts\Routing\ResponseFactory contract.
Trong ResponseFactory interface có 1 method view(), trả về kiểu \Illuminate\Http\Response
interface ResponseFactory
{
/**
* Return a new view response from the application.
*
* @param string $view
* @param array $data
* @param int $status
* @param array $headers
* @return \Illuminate\Http\Response
*/
public function view($view, $data = [], $status = 200, array $headers = []);
}
Trong class \Illuminate\Http\Response sử dụng trait ResponseTrait ở cùng 1 namespace.
class Response extends BaseResponse
{
use ResponseTrait, Macroable {
Macroable::__call as macroCall;
}
}
Trait ResponseTrait hiện thực các method cookie(), header(), riêng widthError() có thể là withException().
trait ResponseTrait
{
public function header($key, $values, $replace = true)
{
$this->headers->set($key, $values, $replace);
return $this;
}
public function cookie($cookie)
{
return call_user_func_array([$this, 'withCookie'], func_get_args());
}
public function withCookie($cookie)
{
if (is_string($cookie) && function_exists('cookie')) {
$cookie = call_user_func_array('cookie', func_get_args());
}
$this->headers->setCookie($cookie);
return $this;
}
public function withException(Exception $e)
{
$this->exception = $e;
return $this;
}
}
Các method của trong trail ResponseTrait đa số trả về $this, theo Builder Pattern.
Tham khảo:
Http Response - Other Response Types
\Illuminate\Contracts\Routing\ResponseFactory
\Illuminate\Http\Response
\Illuminate\Http\ResponseTrait