Middleware

Hi everyone, Let's say I want to redirect the user to a custom page after registration action to complete some steps like company information, plan, ...etc So How can I setup the middleware ? thanks
10 Replies
Dan Harrin
Dan Harrin2y ago
you add the middleware to the middleware array in the config file
BKF Dev
BKF DevOP2y ago
Hi @Dan Harrin thanks for reply, I did it already, but the problem it causes an infinite redirect loop in that custom page, how can I exclude it or is there any other solution ?
Andrew Wallo
Andrew Wallo2y ago
Maybe another option could be to add a wizard on your registration page if nothing else works... Seems pretty similar to what you are asking for
Andrew Wallo
Andrew Wallo2y ago
For example I do it here in this application. Actually very similar to what you are saying... https://github.com/andrewdwallo/erpsaas
GitHub
GitHub - andrewdwallo/erpsaas
Contribute to andrewdwallo/erpsaas development by creating an account on GitHub.
wyChoong
wyChoong2y ago
Exclude redirect when you in that page by using route name..
BKF Dev
BKF DevOP2y ago
How can I do that ? Thanks for the suggestion, but I have other cases where it will happen after consumption some features for example Any solution please dear @Dan Harrin 🥺
awcodes
awcodes2y ago
Would it not be better to use a wizard to have multiple steps for the registration?
BKF Dev
BKF DevOP2y ago
Hi @awcodes thanks for reply, but my case is : I need a middleware to redirect users to a specific filament page for a given reason they do. Let's say to payment page where they have to pay a bill to unlock using the app, I hope I've clearly explained it 🙂
awcodes
awcodes2y ago
Ah. Ok. In that case, to prevent loops you just need to check against things on the $request object to determine you’re logic, ie. Params or current uri, etc. check the laravel docs. It’s not really a filament thing. Simplified example:
public function handle(Request $request, Closure $next): Response|RedirectResponse
{
$currentPath = $request->getRequestUri();

if (
$currentPath !== '/' &&
! Str::of($currentPath)->contains([config('filament.path'), '/livewire', '/storage', '?'])
) {
if (! preg_match('/.+\/$/', $currentPath)) {
return redirect(url($currentPath) . '/');
}
}

return $next($request);
}
public function handle(Request $request, Closure $next): Response|RedirectResponse
{
$currentPath = $request->getRequestUri();

if (
$currentPath !== '/' &&
! Str::of($currentPath)->contains([config('filament.path'), '/livewire', '/storage', '?'])
) {
if (! preg_match('/.+\/$/', $currentPath)) {
return redirect(url($currentPath) . '/');
}
}

return $next($request);
}
BKF Dev
BKF DevOP2y ago
Great! Thanks a lot dear @awcodes 👍
Want results from more Discord servers?
Add your server