Laravel gets supposed URL

I use generic httpRequest to log in, so I could use Redirect::intended(); to bring the user to the URL before they go to the login page. Everything works well.

Now I changed the login to an ajax request, I can only redirect the url to javascript now. So I have to pass the intended URL to the front end and then window.location=url

The problem is that I cannot get the intended/origina l URL. Can any laravel expert help me?

+8
javascript php laravel
source share
4 answers

In your controller action, use:

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

(where ... is the return URL)

Then return it to the JSON response and use window.location or another to do the redirection.

+10
source share

When you show the registration of the form, you can grab the intended url from session , if available, and pass it to the view, and then redirect using window.location .

So. how to capture intended url ?

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

Here is the first argument to the intended url , if it is available in session , and the default is to use the home page using the url('/') helper method url('/') , so $intended_url will always contain the url that is intended or defaulr. Then, when you download the view, go through $intended_url using the following:

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

Then use it in the view, for example:

 window.location = $intended_url; 

Alternatively, you can configure View Composer , so whenever the login view / form is displayed, the intended url will be available in this and you can do this 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 name of the view for the login page, if this is something else in your case, then change it to the corresponding name of your login view. You can save this code in your app/start folder inside the file 'global.php' or save it in a separate file and include this file inside the global.php file using this (at the end):

 require 'view_composer.php'; 

It is assumed that the file name will be view_composer.php , be present in the app/start folder.

+9
source share

In Laravel 5.7:

 $url = redirect()->intended()->getTargetUrl(); 
0
source share

I use the following approach with a user login controller and middleware for Laravel 5.7, but I hope this works in any version of laravel 5

  • in middleware

     if (Auth::check()){ return $next($request); } else{ return redirect()->guest(route('login')); } 
  • if you do not pass the intentional URL to the client side, use the following controller login method

     if (Auth::attempt(['email' => $email, 'password' => $password])) { return redirect()->intended('/default'); } 
  • If you need to pass the intent URL on the 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); } 
  • Client redirect to destination URL

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

All Articles