EditPost force redirect to ViewPost if Post has given condition
Hi, I have some model that has a "closed" condition. Once the Model is closed, it cannot be edited anymore and therefore EditPost shouldn't be accesible.
The decision must be made regarding the state of the post, not the user, so I wanted to create a redirection if someone go to the post and it is "closed".
I tried all kind of hooks, but did not find the solution. I tried to overwrite mount() for example, but it returns nothing, so I don't know how to rise a redirect condition from there. I tried getRedirectUrl from EditRecord class, but this is only to redirect AFTER the record has been saved. I tried to overwrite the render() function...
public function render(): View
{ if ($this->record->isBlocked()) return redirect()->route('filament.app.resources.post.view', ['record' => $this->record]);
return parent::render();
}
but it tells me
Return value must be of type Illuminate\Contracts\View\View, Livewire\Features\SupportRedirects\Redirector returned
I cannot believe one cannot "secure" the edit url redirecting to view if conditions are not met, so I guess I am doing something wrong.
Can anybody help me? Is there a way in Route or Middleware to work with the resource and redirect? Any way to correctly convert a redirect() to a View?
Please, help.
Solution:Jump to solution
You can use $this->redirect() to perform a redirect from the Edit page class mount method, this doesn't have your check in it but this should achieve what you're after:
```php
public function mount(string|int $record): void
{...
3 Replies
Solution
You can use $this->redirect() to perform a redirect from the Edit page class mount method, this doesn't have your check in it but this should achieve what you're after:
Also use the static getUrl method on your resource to generate a URL (if using a seperate panel make sure to pass in the panel id); it's also a lot more readable
Cool! It worked!