如果授权失败,Laravel自定义错误页

如果授权失败,Laravel自定义错误页,laravel,Laravel,当我的编辑请求中授权失败时,我试图在laravel中显示一个自定义403文件 我尝试了biddenresponse(),但结果它被取消了 然后我尝试了failedauthorization(),但这也不能重定向,允许我进行编辑 这是我的申请文件 public function authorize() { $job_posting_id = $this->route('id'); $job_posting = Job_postings::where('id', $job_posti

当我的编辑请求中授权失败时,我试图在laravel中显示一个自定义403文件

我尝试了
biddenresponse()
,但结果它被取消了

然后我尝试了
failedauthorization()
,但这也不能重定向,允许我进行编辑

这是我的申请文件

public function authorize()
{
  $job_posting_id = $this->route('id');

  $job_posting = Job_postings::where('id', $job_posting_id)->where('user_id', user()->id)->exists();

  if($job_posting){
    return true;
  }

  return false;
}

public function failedAuthorization()
{

  return redirect('errors.dashboard.parent.403');
}
我有一个名为errors的文件夹,里面有仪表板文件夹和错误文件所在的父文件夹

如果授权失败,我如何重定向到该页面

public function authorize()
{
  $job_posting_id = $this->route('id');

  $job_posting = Job_postings::where('id', $job_posting_id)->where('user_id', user()->id)->exists();

  if($job_posting){
    return true;
  }

  return redirect('/error');
}

public function failedAuthorization()
{

  return view('errors.dashboard.parent.403');
}
在你的路线上

Route::get('error', 'YourController@failedAuthorization');
Laravel文件:


如果您让一切都像文档中一样,您将实现:

  • url将保持不变
  • 将显示模板
    resources/views/errors/403.blade.php
  • aslo响应的状态代码为:403禁止
  • 是否要将url更改为

    return abort('403')不进行重定向。它只是向客户端显示发生了错误



    在您的情况下,在单个脚本中显示不同的内容比在不同的脚本中显示更容易。尝试将单个错误脚本保留为“入口点”。在该脚本中,为不同的用户更改输出。假设403.blade.php是一个包含大量403个错误页面的布局。

    转到文件Exceptions/Handler.php的
    report
    方法

     public function report(Exception $e)
     {
         if(!Auth::check())
         {
            // Do whatever you want - redirect or return something
         }
         if($e instanceof HttpException && $e->getStatusCode() == 403)
         {
             // Do whatever you want - redirect or return something
         }
        return parent::report($e);
     }
    
    如果授权失败,只需写
    abort(403)


    希望这会有所帮助:)

    在您的laravel文件夹中打开app/Exceptions/Handler.php粘贴以下代码。这将返回403和500错误的自定义错误页

    public function render($request, Exception $exception)
    {
        if ($exception instanceof CustomException) {
            return response()->view('errors.custom_all', [], 500);
        }
    
        if ($exception instanceof \Spatie\Permission\Exceptions\UnauthorizedException) {
            return response()->view('errors.custom', [], 403);
        }
        return parent::render($request, $exception);
    }
    

    返回视图('errors.dashboard.parent.403')
    ?我也尝试过,但它只是不断地传递它并进行编辑。您需要创建一个异常,并在Exceptions/Handler.php文件的报告方法中执行
    if($e-instance of YOUREXCEPTION){//here return your-view}
    @Vikash您能写一个我该怎么做的答案吗?我已经厌倦了编写if handler.php的方法了?你想要任何403代码吗?这对我没有任何帮助,也回答不了这个问题。我已经阅读了文档。为什么我要这样做是因为我有两个错误页面,一个用于未登录用户,另一个用于登录用户,一个文件位于errors/中,另一个位于errors/仪表板中。如何显示不同的错误页面?Vikash,我为什么要这样做是因为我有两个错误页面,一个用于未登录用户,另一个用于登录用户。如何显示不同的错误页面?此外,如果您的答案有帮助,您的答案不会重定向到页面检查一次。您的答案隐式假设OP使用空间权限包,这在问题的任何地方都没有提到。请不要发布代码并告诉它将起作用,请解释代码的作用以及为什么起作用。
    public function render($request, Exception $exception)
    {
        if ($exception instanceof CustomException) {
            return response()->view('errors.custom_all', [], 500);
        }
    
        if ($exception instanceof \Spatie\Permission\Exceptions\UnauthorizedException) {
            return response()->view('errors.custom', [], 403);
        }
        return parent::render($request, $exception);
    }