Support the ongoing development of Laravel.io →
Requests Views
Last updated 2 years ago.
0

You question makes me think a while.

From the technical point of view, in a middleware you have to return an HttpResponse, and the view() helper does (so it is fine).

But I think that the problem is... when you have an HTML request you are not calling the $next() closure, so the other middlewares are not being executed (if has more). In the other hand I really can't imagine a different way to do what you want if it is not in every controller, checking if is HTML or JSON.

In conclusion, I think that you are doing wll, just be sure that your middleware is executed at the last of all.

Hope it helps :P

0

JuanDMeGon said:

In the other hand I really can't imagine a different way to do what you want if it is not in every controller, checking if is HTML or JSON.

Instead of returning a view, I'd redirect to another Controller which returns view.

0

In Laravel 5, you'll need to wrap this in a Illuminate\Http\Response. So something like this:

use Illuminate\Http\Response;

in the header of your file and in your handler

return new Response(view('view.path'));

Otherwise it won't work.

jayakt97 liked this reply

1

It's work for me:

return response(view('view.path'));

jayakt97 liked this reply

1

Yeah

return response(view('view.path'));

Works totally for me ! Thanks !

0

Sign in to participate in this thread!

Eventy

Your banner here too?

abisz abisz Joined 28 Aug 2015

Moderators

We'd like to thank these amazing companies for supporting us

Your logo here?

Laravel.io

The Laravel portal for problem solving, knowledge sharing and community building.

© 2024 Laravel.io - All rights reserved.