Laravel get intended url

前端 未结 4 745
悲哀的现实
悲哀的现实 2020-12-31 11:08

I use a common httpRequest to login, so I could use Redirect::intended(); to lead the user to a url before them being lead to the login page. That all works we

4条回答
  •  长发绾君心
    2020-12-31 11:43

    When you are showing the form foe log in, you can grab the intended url from session if available and pass it to the view then redirect using window.location.

    So. how to grab the intended url ?

    $intended_url = Session::get('url.intended', url('/'));
    Session::forget('url.intended');
    

    Here, first argument is intended url if available in the session and default is set to home page using url('/') helper method, so the $intended_url will always contain a url, intended or defaulr. Then when you are loading the view, pass the $intended_url using this:

    return View::make('login')->with('intended_url', $intended_url);
    

    Then use it from the view like:

    window.location = $intended_url;
    

    Alternatively, you may setup a View Composer so whenever the login view/form is displayed the intended url will be available in that view and you can do it using this:

    View::composer('login', function($view){
        $intended_url = Session::get('url.intended', url('/'));
        Session::forget('url.intended');
        return $view->with('intended_url', $intended_url);
    });
    

    Here, login is the view name for login page, if this is something else in your case then change it to the appropriate name of your login view. You can keep this code in your app/start folder inside the 'global.php' file or keep it in a separate file and include this fie inside global.php file using this (at the end):

    require 'view_composer.php';
    

    Assumed that, file name would be view_composer.php, present in the app/start folder.

提交回复
热议问题