F
Filament10mo ago
Anish

Middleware in Panel Route

I have a panel with a couple of resources and a couple of custom pages. I want to apply middleware in one of the custom pages, not on all of them. To give some context, I am developing a student application portal and there is a form submission page. But before form submission, I want to run checks (date not over, payment completed etc.) that the form can be submitted. If those checks are not satisfied, the user should not be able to access the page. Also in one of the resources, I want to restrict access to a view page. I have a policy which restricts the access to the view page. However that returns unauthorised access, but I don't want to show such a page, instead I will like to send the user to the dashboard with some notifications. For regular (non livewire) pages, middleware with can and redirect will do the job. My question is how do I achieve this for a resource page? Thanks for any help in advance.
2 Replies
David | Fortune Validator
Following. I need a similar setup.
Anish
AnishOP10mo ago
Found a hacky solution - don't like it but it works. I created a middleware, which I name as StudentPanelCheckMiddleware. In that I run a try catch block. In the try block -
try {
$routeName = $request->route()->getName();

if ($routeName === RouteHelper:: getRouteName( 'student', 'form-submit') )
{

// Now run checks
throw_if ( GlobalConfig::afterPaymentDate (), 'Payment Date is over.');
// More checks ...
}
catch ($e)
{

Notification::make()
->label($e->getMessage ())
->danger()
->send();

return redirect()->route( RouteHelper::getDashboardRouteName ('student'));

}
try {
$routeName = $request->route()->getName();

if ($routeName === RouteHelper:: getRouteName( 'student', 'form-submit') )
{

// Now run checks
throw_if ( GlobalConfig::afterPaymentDate (), 'Payment Date is over.');
// More checks ...
}
catch ($e)
{

Notification::make()
->label($e->getMessage ())
->danger()
->send();

return redirect()->route( RouteHelper::getDashboardRouteName ('student'));

}
You will also need to add it to the list of middlewares in the panel.
Want results from more Discord servers?
Add your server