dream04110 2016-10-27 15:14
浏览 41

授权失败后,laravel不会重定向

In my requests file in laravel 5.2, I'm setting a condition to check if user is authorized for a request or not.

Code

<?php
namespace App\Http\Requests;
use App\Http\Requests\Request;
use App\questions;
use Auth;
class answerAddRequest extends Request
{
    public function authorize()
    {
        if(questions::where('people_id',Auth::id())->count()==0)
            return true;
        else
        {
            return false;
            return redirect('dashboard');
        }
    }
    public function rules()
    {
        return [
            'answer'=>'required|min:3'
        ];
    }
}

If I do not add return false it executes and inserts in a new row, which shouldn't happen, but with the current code it says Forbidden which is correct but it doesn't redirect to my dashboard view. How can I make it to redirect to some other page if authorization fails?

  • 写回答

2条回答 默认 最新

  • dtn36013 2016-10-27 15:21
    关注

    You are using two return statements in your else block

     else
     {
        return false;
        return redirect('dashboard');
     }
    

    The second return statement is never reachable.

    Update

    If you want to redirect the user then you can create your custom middleware and write same code as you are doing in authorize().

    Use this middleware in routes where you want to redirect the unauthorized user.

    评论

报告相同问题?

悬赏问题

  • ¥15 关于#网络安全#的问题:求ensp的网络安全,不要步骤要完成版文件
  • ¥15 可否在不同线程中调用封装数据库操作的类
  • ¥20 使用Photon PUN2解决游戏得分同步的问题
  • ¥15 微带串馈天线阵列每个阵元宽度计算
  • ¥15 keil的map文件中Image component sizes各项意思
  • ¥30 BC260Y用MQTT向阿里云发布主题消息一直错误
  • ¥20 求个正点原子stm32f407开发版的贪吃蛇游戏
  • ¥15 划分vlan后,链路不通了?
  • ¥20 求各位懂行的人,注册表能不能看到usb使用得具体信息,干了什么,传输了什么数据
  • ¥15 Vue3 大型图片数据拖动排序