duanji4449 2017-12-13 22:32
浏览 72
已采纳

每个页面上的Laravel 5.5登录表单,刷新而不是重定向

For a web project that requires a login/register system and lot of CRUD operations, I choose to learn Laravel. I have some experience from school with MVC .NET and Spring and while there are a lot of similarities, some things are a little bit different... The requirement of this project is that it has a login form on every page when not logged in (guest). That form changes to "the profile" of the user when logged in (auth). The problem I'm having is that when I log in and try to redirect using the middleware RedirectIfAuthenticated, I come in an infinite loop of redirects for some reason. Is there a solution to create a login form on every page, and instead of redirecting to another view, just refresh the page and set the correct values using Blade's templating engine? I've created the base with the php artisan make:auth command, and made some changes myself. I'll post snippets of what I have with the according class.

To give an idea how it's gonna look & why there need to be a form on every page: (btw I'm putting it in the footer in layouts/app.blade.php. example image

Routes/web.php: I unraffeld the Facade Auth::routes() to this so I can make changes. I left login temporarly but it should be gone when I got my things working. The bottom 2 comments I ommitted.

Route::get('/', 'HomeController@index')->name('home');
Route::get('home', 'HomeController@index')->name('home');

Route::get('teams', 'HomeController@teams')->name('teams');
Route::get('schedules', 'HomeController@schedules')->name('schedules');
Route::get('tables', 'HomeController@tables')->name('tables');
Route::get('rules', 'HomeController@rules')->name('rules');

Route::get('login', 'Auth\LoginController@showLoginForm')->name('login');
Route::post('login', 'Auth\LoginController@login');
Route::post('logout', 'Auth\LoginController@logout')->name('logout');

// Registration Routes...

// Password Reset Routes...

HomeController.php

Here I'm doubting the configuration in the constructor should it be 'guest' or 'auth' as middleware?

    class HomeController extends Controller
    {
    /**
     * Create a new controller instance.
     *
     * @return void
     */
    public function __construct()
    {
        $this->middleware('guest');
    }

    /**
     * Show the homepage.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        return view('home');
    }

    //other pages omitted
    }

LoginController.php (no difference with artisan auth):

class LoginController extends Controller
{
    /*
    |--------------------------------------------------------------------------
    | Login Controller
    |--------------------------------------------------------------------------
    |
    | This controller handles authenticating users for the application and
    | redirecting them to your home screen. The controller uses a trait
    | to conveniently provide its functionality to your applications.
    |
    */

    use AuthenticatesUsers;

    /**
     * Where to redirect users after login.
     *
     * @var string
     */
    protected $redirectTo = '/home';

    /**
     * Create a new controller instance.
     *
     * @return void
     */
    public function __construct()
    {
        $this->middleware('guest')->except('logout');
    }
}

RedirectIfAuthenticated.php:

class RedirectIfAuthenticated
{
    /**
     * Handle an incoming request.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \Closure  $next
     * @param  string|null  $guard
     * @return mixed
     */
    public function handle($request, Closure $next, $guard = null)
    {
        if (Auth::guard($guard)->check()) {
            return redirect('home');
        }

        return $next($request);
    }
}

App.blade.php:

@if (Auth::guest())

    //login form omitted ...

@else

   <!-- TODO: check for role & display correct nav -->
   <h2>Logged in!</h2>

  <form id="logout-form" action="{{ url('/logout') }}" method="POST" style="display: none;">
      {{ csrf_field() }}
  </form>
 @endif

Tell me if you need more info. Thanks in advance.

  • 写回答

1条回答 默认 最新

  • dql7588 2017-12-14 03:49
    关注

    In your web.php routes file you declared the home route twice:

    Route::get('/', 'HomeController@index')->name('home');
    Route::get('home', 'HomeController@index')->name('home');
    

    I'd change that to:

    Route::get('/', 'HomeController@index')->name('home');
    

    Then in your LoginController you'll need to update the $redirectTo to reflect that change. So:

    protected $redirectTo = '/';
    

    Same for your RedirectIfAuthenticated middleware:

    if (Auth::guard($guard)->check()) {
        return redirect('/');
    }
    

    Last but not least, your HomeController middleware is incorrect. guest is for routes that are only accessible when unauthenticated, such as login, register or forget password. But since you want the routes from your HomeController to only be accessible by authenticated users, then you'd have to change the middleware to auth:

    public function __construct()
    {
        $this->middleware('auth');
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 乌班图ip地址配置及远程SSH
  • ¥15 怎么让点阵屏显示静态爱心,用keiluVision5写出让点阵屏显示静态爱心的代码,越快越好
  • ¥15 PSPICE制作一个加法器
  • ¥15 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 R语言卸载之后无法重装,显示电脑存在下载某些较大二进制文件行为,怎么办
  • ¥15 java 的protected权限 ,问题在注释里