Laravel 获取预期网址

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21499444/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 08:57:19  来源:igfitidea点击:

Laravel get intended url

javascriptphplaravel

提问by Adams.H

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 well.

我使用一个通用的 httpRequest 来登录,所以我可以Redirect::intended();在用户被引导到登录页面之前将其引导到一个 url。这一切都很好。

Now I've changed login to ajax request I can only redirect the url in javascript now. So I've to pass the intended url to front end then do the window.location=url

现在我已将登录更改为 ajax 请求,我现在只能在 javascript 中重定向 url。所以我必须将预期的 url 传递给前端然后执行window.location=url

The problem is I can't get the intended/original url. Can any kind laravel expert help me out ?

问题是我无法获得intended/original 网址。任何好心的 Laravel 专家都可以帮助我吗?

回答by thomaspaulb

In your controller action use:

在您的控制器操作中使用:

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

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

(Where ...is the fallback url)

...后备网址在哪里)

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

然后在 JSON 响应中返回它,并使用window.location或其他来进行重定向。

回答by The Alpha

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

当您显示登录表单时,您可以urlsession可用的位置获取预期内容并将其传递给视图,然后使用window.location.

So. how to grab the intended url?

所以。怎么抢intended url

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

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

在这里,第一个参数是intended url如果可用,session并且默认设置为使用url('/')helper 方法的主页,因此$intended_url将始终包含url、预期或默认值。然后当你加载视图时,$intended_url使用这个传递:

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 Composerso whenever the loginview/form is displayed the intended urlwill be available in that view and you can do it using this:

或者,您可以设置一个View Composer,以便每当login显示视图/表单时,该视图/表单都intended url将在该视图中可用,您可以使用以下方法进行操作:

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

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

这里login是登录页面的视图名称,如果这是您的其他情况,请将其更改为您的login视图的适当名称。您可以将此代码保存app/start在“global.php”文件内的文件夹中,或将其保存在单独的文件中,并global.php使用此文件(最后)将此文件包含在文件中:

require 'view_composer.php';

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

假设,文件名将是view_composer.php, 出现在app/start文件夹中。

回答by user580485

In Laravel 5.7:

在 Laravel 5.7 中:

$url = redirect()->intended()->getTargetUrl();

回答by Nayeem Azad

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

我将以下方法与 Laravel 5.7 的自定义登录控制器和中间件一起使用,但我希望它适用于任何 Laravel 5 版本

  • 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;
                  }
           },
    
  • 中间件内部

    if (Auth::check()){
        return $next($request);
    }
    else{
      return redirect()->guest(route('login'));
    }
    
  • 如果您没有将意图的 url 传递给客户端,请使用以下内部控制器登录方法

    if (Auth::attempt(['email' => $email, 'password' => $password])) {
    return redirect()->intended('/default');
    }
    
  • 如果您需要将意图的 url 传递给客户端,您可以尝试以下操作

       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);
        }
    
  • 从客户端重定向到预期的 url

           success: function (data) {               
                  if(data.redirectUrl){
                  window.location.href = data.redirectUrl;
                  }
           },