首页 > 解决方案 > 自定义拒绝访问警告

问题描述

如果您没有正确的角色,如何正确重定向此访问禁令?通知非常简单,显示错误,我希望它通过通知重定向到家。

namespace jeremykenedy\LaravelRoles\App\Exceptions;

class RoleDeniedException extends AccessDeniedException
{
    /**
     * Create a new role denied exception instance.
     *
     * @param string $role
     */
    public function __construct($role)
    {
        $this->message = sprintf("You don't have a required ['%s'] role.", $role);
    }
}

标签: laravel

解决方案


覆盖类render的函数RoleDeniedException

    /**
     * Render the exception into an HTTP response.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function render($request)
    {
        return redirect('home');
    }

有关更多详细信息,请参阅以下链接;

https://laravel.com/docs/8.x/responses#redirects

https://laravel.com/docs/8.x/helpers#method-redirect


推荐阅读