Pomoc - Szukaj - Użytkownicy - Kalendarz
Pełna wersja: [Laravel] Jak usawić przkierowanie
Forum PHP.pl > Forum > PHP > Frameworki
tomi0001
Mam taki problem testuje sobie framework 4 i nie wiem jak ustawić sobie w routingu login tak, żeby w tym monecie kiedy użytkownik jest zalogowany, żeby przekierowywał na stronę główną czyli chodzi o to, że jak ktoś wejdzie na stronę(jeżeli jest zalogowany) 127.0.0.1/lara/login to, że nie wyświetlał pola do logowania tylko przeszedł na stronę główną
plik routing.php
  1. Route::post('login', function()
  2. {
  3. //kombinował z tym, ale to nie to
  4. //if (Auth::check())
  5. //{
  6. //return View::make('profile')->with('user',
  7. //Auth::user());
  8. //}
  9. $user = array(
  10. 'email' => Input::get('email'),
  11. 'password' => Input::get('password')
  12. );
  13. if (Auth::attempt($user))
  14. {
  15.  
  16. return Redirect::to('profile');
  17. }
  18. return Redirect::to('login')->with('login_error','Logowanie nie powiodło się.');
  19. });


plik login.php
  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <title>Uwierzytelnianie w Laravelu - logowanie</title>
  5. <meta charset="utf-8">
  6. </head>
  7. <body>
  8. <h2>Uwierzytelnianie w Laravelu - logowanie</h2>
  9. <?= '<span style="color:red">' .
  10. Session::get('login_error') . '</span>' ?>
  11. <?= Form::open() ?>
  12. <?= Form::label('email', 'Adres email: ') ?>
  13. <?= Form::text('email', Input::old('email')) ?>
  14. <br>
  15. <?= Form::label('password', 'Hasło: ') ?>
  16. <?= Form::password('password') ?>
  17. <br>
  18. <?= Form::submit('Zaloguj!') ?>
  19. <?= Form::close() ?>
  20. </body>
  21. </html>
markonix


  1. public function __construct()
  2. {
  3. $this->middleware('guest', ['except' => 'getLogout']);
  4. }


  1. 'guest' => \App\Http\Middleware\RedirectIfAuthenticated::class,


No i w końcu ten Middleware:
RedirectIfAuthenticated.php

  1. <?php
  2.  
  3. namespace App\Http\Middleware;
  4.  
  5. use Closure;
  6. use Illuminate\Contracts\Auth\Guard;
  7.  
  8. class RedirectIfAuthenticated
  9. {
  10. /**
  11.   * The Guard implementation.
  12.   *
  13.   * @var Guard
  14.   */
  15. protected $auth;
  16.  
  17. /**
  18.   * Create a new filter instance.
  19.   *
  20.   * @param Guard $auth
  21.   * @return void
  22.   */
  23. public function __construct(Guard $auth)
  24. {
  25. $this->auth = $auth;
  26. }
  27.  
  28. /**
  29.   * Handle an incoming request.
  30.   *
  31.   * @param \Illuminate\Http\Request $request
  32.   * @param \Closure $next
  33.   * @return mixed
  34.   */
  35. public function handle($request, Closure $next)
  36. {
  37. if ($this->auth->check()) {
  38. return redirect('/panel');
  39. }
  40.  
  41. return $next($request);
  42. }
  43. }


Przekieruje do panelu. Rozwiązanie z 5tki, nie mam pewności czy w 4 też tak się to robi ale może Cię to naprowadzi.
tomi0001
A te dwa pierwsze do jakiego pliku?
markonix
Pierwszy fragment to konstruktor AuthControllera.

Drugi w pliku Kernel.
$routeMiddleware
To jest wersja lo-fi głównej zawartości. Aby zobaczyć pełną wersję z większą zawartością, obrazkami i formatowaniem proszę kliknij tutaj.
Invision Power Board © 2001-2025 Invision Power Services, Inc.