Skip to content

[Q] How to add custom header? #679

Answered by tarampampam
francsiswanto asked this question in Q&A
Discussion options

You must be logged in to vote

Hi @francsiswanto!

You should not use header() function for header setting - Middleware will be great for this. Something like that:

namespace App\Http\Middleware;

class AppendStartedAtHeaderMiddleware
{
    /**
     * Handle an incoming request.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \Closure  $next
     * @return mixed
     */
    public function handle($request, Closure $next)
    {
        /** @var \Illuminate\Http\Response $response */
        $response = $next($request);

        $response->header('PHP-Start', date("Y-m-d H:i:s", LARAVEL_START));

        return $response;
    }
}

More docs about middlewares: https://laravel.com/docs/8.x/middleware…

Replies: 2 comments 5 replies

Comment options

You must be logged in to vote
5 replies
@francsiswanto
Comment options

@rustatian
Comment options

@francsiswanto
Comment options

@wolfy-j
Comment options

@francsiswanto
Comment options

Answer selected by francsiswanto
Comment options

You must be logged in to vote
0 replies
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
A-plugin Area: module R-question Question
4 participants