How do you add headers to a response with a middleware?

0 votes

I can't figure out how to how to add headers to a response from a middleware. I've used both ->header(...) and ->headers->set(...) but both gives errors. So how do you do it?

First I tried with

public function handle($request, Closure $next) {
    $response = $next($request);

    $response->headers->set('refresh', '5;url=' . route('foo'));

    return $response;
}

which is the same as in Illuminate\Http\Middleware\FrameGuard.php, but that gives

Call to a member function set() on a non-object

Second I tried with

public function handle($request, Closure $next) {
    $response = $next($request);

    $response->header('refresh', '5;url=' . route('foo'));

    return $response;
}

But that gives

Method [header] does not exist on view.

So how do you add headers from a middleware?

Oct 28 in Laravel by kartik
• 35,300 points
23 views

1 answer to this question.

0 votes

Hello @kartik,

Using the response helper.

use Illuminate\Http\RedirectResponse;

$response = $next($request);
$response = $response instanceof RedirectResponse ? $response : response($response);

return $response->header('refresh', '5;url=' . route('foo'));

Hope it helps!!

answered Oct 28 by Niroj
• 76,540 points

Related Questions In Laravel

+2 votes
1 answer

How to add a new column to existing table of laravel in a migration?

Hii @kartik, To create a migration, you may ...READ MORE

answered Mar 30 in Laravel by Niroj
• 76,540 points
16,215 views
0 votes
1 answer

How to solve expected response code 220 but got code “”, with message “” in Laravel?

Hii @kartik, if you are using Swift Mailer: ...READ MORE

answered Mar 31 in Laravel by Niroj
• 76,540 points
7,605 views
0 votes
1 answer

How Can I Set the Default Value of a Timestamp Column to the Current Timestamp with Laravel Migrations?

Hello, To create both of the created_at and updated_at columns: $t->timestamp('created_at')->default(DB::raw('CURRENT_TIMESTAMP')); $t->timestamp('updated_at')->default(DB::raw('CURRENT_TIMESTAMP on update ...READ MORE

answered Apr 2 in Laravel by Niroj
• 76,540 points
3,351 views
0 votes
1 answer

How do I write to the console from a Laravel Controller?

Hello @kartik, This can be done with the ...READ MORE

answered Aug 11 in Laravel by Niroj
• 76,540 points
93 views
+1 vote
1 answer

How to make anchor tag with routing using Laravel?

Hey @kartik, First you have to go to ...READ MORE

answered Mar 18 in Laravel by Niroj
• 76,540 points
3,434 views
0 votes
1 answer

What is redirection in Laravel?

Named route is used to give specific ...READ MORE

answered Mar 18 in Laravel by Niroj
• 76,540 points
411 views
0 votes
1 answer

How to install Laravel via composer?

Hello, This is simple you just need to ...READ MORE

answered Mar 23 in Laravel by Niroj
• 76,540 points
281 views
+1 vote
1 answer

What are named routes in Laravel and How can specify route names for controller actions?

Hey @kartik, Named routing is another amazing feature of ...READ MORE

answered Mar 23 in Laravel by Niroj
• 76,540 points
4,140 views
0 votes
1 answer

How do you check if a field is not null with Eloquent?

Hello @kartik, Simply,we can use Model::whereNotNull('sent_at'); Or Model::whereRaw('sent_at is not null'); Thank ...READ MORE

answered Nov 11 in Laravel by Niroj
• 76,540 points
15 views
0 votes
1 answer

How to make a new page with routing using Laravel?

Hey @kartik, First you have to go to ...READ MORE

answered Mar 18 in Laravel by Niroj
• 76,540 points
440 views