Laravel get intended url

前端 未结 4 729
悲哀的现实
悲哀的现实 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:29

    In your controller action use:

    $url = Redirect::intended( ... )->getTargetUrl();

    (Where ... is the fallback url)

    Then return it in the JSON response, and use window.location or other to do the redirect.

    0 讨论(0)
  • 2020-12-31 11:29

    In Laravel 5.7:

    $url = redirect()->intended()->getTargetUrl();
    
    0 讨论(0)
  • 2020-12-31 11:41

    I am using the following approach with a custom login controller and middleware for Laravel 5.7, but I hope that works in any of laravel 5 versions

    • inside middleware

      if (Auth::check()){
          return $next($request);
      }
      else{
        return redirect()->guest(route('login'));
      }
      
    • if you are not passing the intented url to client side use the following inside controller login method

      if (Auth::attempt(['email' => $email, 'password' => $password])) {
      return redirect()->intended('/default');
      }
      
    • If you need to pass the intented url to client side, you can try the following

         if (Auth::attempt(['username' => $request->username, 'password' => $request->password])) {
             $intended_url= redirect()->intended('/default')->getTargetUrl();
             $response = array(
            'status' => 'success',
            'redirectUrl' => $intended_url,
            'message' => 'Login successful.you will be redirected to home..', );
            return response()->json($response);
          } else {
              $response = array(
            'status' => 'failed',
            'message' => 'username or password is incorrect', );
           return response()->json($response);
          }
      
    • Redirect to the intented url from clientside

             success: function (data) {               
                    if(data.redirectUrl){
                    window.location.href = data.redirectUrl;
                    }
             },
      
    0 讨论(0)
  • 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.

    0 讨论(0)
提交回复
热议问题