dtds8802 2015-09-15 13:44
浏览 11
已采纳

Laravel。 登录评论

I have to create ,,log in to comment" button. On click this button should redirect user to login page and after successfull loging in, he should be redirected to page, where he have clicked that button. I tried to make fake url with auth middleware around it like this:

Route::group(['middleware' => 'auth'], function() {
    Route::get('/log-in-to-comment', 'UserController@getLogInToComment');
});

I have defined my getLogInToComment function like this:

public function getLogInToComment() {
        return redirect()->back();
}

But it redirects me to /log-in-to-comment which is already in filter so I get too many redirects. Also I have tried return Redirect::to(URL::previous()); but it also doesn't work. As far as I know, post method should not be used on redirects, so how do I deal with this simple task? Thanks in advance!

  • 写回答

2条回答 默认 最新

  • donglie1994 2015-09-15 15:00
    关注

    Since Laravel always redirects back to the page you came from (which is in your case /log-in-to-comment), you have to overwrite this URL.

    A possible way to do so is to fetch the URL of the last site and an save it to the browsers session. To do so, make sure the getLogInToComment-route is not using auth or guest middleware, since it should be processed as a guest as well as a signed in user. Then, store the previous site if the user is not logged in, and redirect back to it if he is.

    public function getLogInToComment() {
        if (!Auth::user()) {
            // User is not logged in. Save previous URL and redirect to login
            Session::put('prev', URL::previous());
            return Redirect::route('login');
        } else {
            // User is now logged in and returned to this route again. Redirect to site
            return Redirect::to(Session::get('prev'));
        }
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 关于大棚监测的pcb板设计
  • ¥15 stm32开发clion时遇到的编译问题
  • ¥15 lna设计 源简并电感型共源放大器
  • ¥15 如何用Labview在myRIO上做LCD显示?(语言-开发语言)
  • ¥15 Vue3地图和异步函数使用