Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <h2>For Laravel 5.3 and above</h2> <p>Check <a href="https://stackoverflow.com/a/39595605/908579">Scott's answer</a> below.</p> <h2>For Laravel 5 up to 5.2</h2> <p>Simply put,</p> <p>On auth middleware:</p> <pre class="lang-php prettyprint-override"><code>// redirect the user to "/login" // and stores the url being accessed on session if (Auth::guest()) { return redirect()-&gt;guest('login'); } return $next($request); </code></pre> <p>On login action:</p> <pre class="lang-php prettyprint-override"><code>// redirect the user back to the intended page // or defaultpage if there isn't one if (Auth::attempt(['email' =&gt; $email, 'password' =&gt; $password])) { return redirect()-&gt;intended('defaultpage'); } </code></pre> <h2>For Laravel 4 (old answer)</h2> <p>At the time of this answer there was no official support from the framework itself. Nowadays you can use <del>the method pointed out by bgdrl below</del> this method: <em>(I've tried updating his answer, but it seems he won't accept)</em></p> <p>On auth filter:</p> <pre class="lang-php prettyprint-override"><code>// redirect the user to "/login" // and stores the url being accessed on session Route::filter('auth', function() { if (Auth::guest()) { return Redirect::guest('login'); } }); </code></pre> <p>On login action:</p> <pre class="lang-php prettyprint-override"><code>// redirect the user back to the intended page // or defaultpage if there isn't one if (Auth::attempt(['email' =&gt; $email, 'password' =&gt; $password])) { return Redirect::intended('defaultpage'); } </code></pre> <h2>For Laravel 3 (even older answer)</h2> <p>You could implement it like this:</p> <pre class="lang-php prettyprint-override"><code>Route::filter('auth', function() { // If there's no user authenticated session if (Auth::guest()) { // Stores current url on session and redirect to login page Session::put('redirect', URL::full()); return Redirect::to('/login'); } if ($redirect = Session::get('redirect')) { Session::forget('redirect'); return Redirect::to($redirect); } }); </code></pre> <pre class="lang-php prettyprint-override"><code>// on controller public function get_login() { $this-&gt;layout-&gt;nest('content', 'auth.login'); } public function post_login() { $credentials = [ 'username' =&gt; Input::get('email'), 'password' =&gt; Input::get('password') ]; if (Auth::attempt($credentials)) { return Redirect::to('logged_in_homepage_here'); } return Redirect::to('login')-&gt;with_input(); } </code></pre> <p>Storing the redirection on Session has the benefit of persisting it even if the user miss typed his credentials or he doesn't have an account and has to signup.</p> <p>This also allows for anything else besides Auth to set a redirect on session and it will work magically.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload