I am building an application using laravel 10 and i don't want the back button in the browser to work. I created the middleware "PreventBackHistory", and configure it in the Kernel, and included it in the web route. But there is error in the middleware "undefined method header". And the back button is not disabled.
i used this codes
I get error on the header in middleware, so the PreventBackHistory is not working. I need help to enable PreventBackHistory on my codes. Thank you.
PreventBackHistory middleware
namespace App\Http\Middleware;
use Closure;
use Illuminate\Http\Request;
use Symfony\Component\HttpFoundation\Response;
class PreventBackHistory{
/**
* Handle an incoming request.
*
* @param \Closure(\Illuminate\Http\Request): (\Symfony\Component\HttpFoundation\Response)
$next
*/
public function handle(Request $request, Closure $next): Response {
$response = $next($request);
return $response->header('Cache-Control','nocache,no-store,max-age=0;must-revalidate')
->header('Pragma','no-cache')
->header('Expires','Sun, 02 Jan 1990 00:00:00 GMT');
}
}
kernel
protected $middlewareAliases = [ 'PreventBackHistory' => \App\Http\Middleware\PreventBackHistory::class, ];
web route
Route::group(['middleware' => 'admin','PreventBackHistory'], function() {
Route::get('admin/welcome', [WelcomeController::class, 'welcome']);
Route::get('admin/dashboard', [DashboardController::class, 'dashboard']);
});