title: How to redirect login plus previous URL in laravel
body: <pre><code>                                        Laravel 5.1
  </code></pre>
  
  <p>I'm trying to build this functionality for every method in my controller. And this is very unpractical to do and very difficult to maintain. How can I set this that is when I registered an <code>auth</code> middleware in a specific route then it will redirect into a login page together with the URL that trying to view/visit.</p>
  
  <p><strong>Example</strong>:<br>
  URL: <code>public/users</code></p>
  
  <p>If this URL will try to visit by an unauthenticated user, then the user will be redirected to a url like <code>public/login?url=http://localhost/myproject/public/users</code></p>
  
  <p>Then After the user loggedin successfully, the user then will be redirected into <code>http://localhost/myproject/public/users</code></p>
  
  <p><strong>What I have now:</strong>  <em>(But I think not a good one to use)</em></p>
  
  <pre><code>public function getLoginURL(){
      if(!Input::get('location-login')){
          // URL is not set, redirect user to home.
          return redirect('/');
      }
  
      if(auth()-&gt;check()){
          // User is logged in. Go the specified URL (depends on the middleware of the url (route))
          return redirect()-&gt;to(Input::get('location-login'));
      }
  
      if(!auth()-&gt;check()){
          // Redirect user to a login with the URL provided
          return view('pages::auth.login.url');
      }
  }
  </code></pre>