rap-2-h rap-2-h - 2 years ago 102
PHP Question

Which part sets `Content-Type` to `text/HTML` in response

Here is some Laravel code for a route:

Route::get('a', function() {
return 'hello';

Calling this route, the
header value is

Where does this default behavior come from? Is it set by:

  • Nginx?

  • PHP?

  • Laravel?

Answer Source

Laravel extends Symphony's requests/responses but is still tied heavily on their default behaviour. This particular feature is based on Symphony's Response::prepare function.

public function prepare(Request $request)
    //does a lot of things, would not go into detail
        // Content-type based on the Request
        if (!$headers->has('Content-Type')) {
            $format = $request->getRequestFormat();
            if (null !== $format && $mimeType = $request->getMimeType($format)) {
                $headers->set('Content-Type', $mimeType);
 //Does a lot more things

This is based on the $request->getMimeType($format) function and $format comes from $request->getRequestFormat()

public function getRequestFormat($default = 'html')
    if (null === $this->format) {
        $this->format = $this->attributes->get('_format');
    return null === $this->format ? $default : $this->format;

Note how the default is "html" when there's no request format explicitly set. It also will only be set if you haven't already explicitly set it.

The mime types are based on a lookup initialized at https://github.com/symfony/http-foundation/blob/master/Request.php#L1844

The framework does go a level beyond this and provide alternative response types like e.g. JsonResponse (what laravel uses when you do response()->json()) and this response uses a different default.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download