Sometimes you may wish to redirect the user to another location, either to an external site or within the store. To do so, pass the URL, route(), or an Illuminate\Http\RedirectResponse instance to the setRedirect() method. For example, if you stored the age of customers (computed from their provided date of birth), and needed to prevent access to those under the age of 18:
\Aero\Store\Http\Responses\ProductPage::extend(function ($page) {
if ($page->request->customer()->age < 18) {
$page->setRedirect('/denied');
}
});
As extension code is evaluated in the order it's added to the response builder, code that runs after this step may override the redirect. To force a redirect and prevent execution of the code that follows in the pipeline, you should return an instance of the redirect() helper:
\Aero\Store\Http\Responses\ProductPage::extend(function ($page) {
if (! $page->product->canBeViewedByIp($page->request->ip())) {
return redirect('/denied');
}
});