在自定义请求类中验证后如何自定义重定向

时间:2018-11-30 21:39:07

标签: laravel

我创建了自定义请求类,在该类中,我禁止访问特定路由。假设我试图禁止通过此请求类删除某些自定义类别。一切正常,我被重定向到403页。问题是,我如何自定义该重定向路径?

我查看了Internet,看到提到了bidbiddenResponse()方法,但是我无法使其正常工作。

我正在使用Laravel 5.7

2 个答案:

答案 0 :(得分:0)

如果您使用的是命名路由,例如

public partial class frmhome : Form
    {
        public frmhome()
        {
            InitializeComponent();
        }


        //Create the instant variable 
        MySqlConnection mysqlConn;
        MySqlDataAdapter mysqlAdapt;
        MySqlDataReader mysqlRead;
        MySqlCommand mysqlComm;

        //Create the Memory Variable         
        String qry;
        //add member
        private void Home_Load(object sender, EventArgs e)
        {
            panelmemreg.Visible = true;
            panelAddbook.Visible = false;
            panelissue.Visible = false;
            panelres.Visible = false;


            //Using Error handler tool 
            try
            {
                //Create the Memory Variable 
                string srv, db, user, pass;
                //Assigning the values 
                srv = "localhost"; db = "lms"; user = "root"; pass = "";
                //assigning the command 
                qry = "SERVER=" + srv + ";" + "DATABASE=" + db + ";" + "UID=" + user + ";" + "PASSWORD=" + pass + ";";
                mysqlConn = new MySqlConnection(qry);
            }
            catch (Exception ex)
            {
                //Display Error Message 
                MessageBox.Show("Couldn't connect :" + ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }

        }

        private void btnAdd_Click(object sender, EventArgs e)
        {
            //using the error handler tool 
            try
            {

                //Set the sql statement 
                qry = "INSERT INTO member (Member id,Member Name,Contact No,Address,Reg.date)VALUES('" + txtbid.Text + "','" + txtbname.Text + "','" + txtbtelno.Text + "','" + txtbaddr.Text + "','" + dtpreg.Value.Date + "')";
                //Open the connection 
                mysqlConn.Open();
                //Set the statement in command control 
                mysqlComm = new MySqlCommand(qry, mysqlConn);
                //Execute the statement 
                mysqlComm.ExecuteNonQuery();
                String msg3 = "Add new member Successfully";
                lblStatus.Text = msg3;

            }


            finally
            {
                //close the connection 
                mysqlConn.Close();
            }

        }

然后您可以执行此操作。

Route::get("/user", "UserController@index")->name('user');

答案 1 :(得分:0)

我找到了一些有关此的信息。从Laravel 5.4 forbiddenResponse开始,功能已被删除。

通过更改位于Handler.php文件夹中的App\Exceptions文件,我设法完成了我需要的工作。在渲染功能中,我正在检查Exception是否为AuthorizationException,之后我将重定向到另一个页面。

public function render($request, Exception $exception)
    {
        if ($exception instanceof \Illuminate\Auth\Access\AuthorizationException) {
            return response()->redirectTo(route('categories.index'));
        }
        return parent::render($request, $exception);
    }