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

问题描述:

对于需要登录/注册系统和大量CRUD操作的Web项目,我选择学习Laravel.我从学校学习过MVC .NET和Spring的经验,虽然有很多相似之处,但有些地方有些不同...该项目的要求是,未登录时每个页面上都有一个登录表单(来宾).登录(身份验证)后,该表格将更改为用户的个人资料".我遇到的问题是,当我登录并尝试使用中间件RedirectIfAuthenticated进行重定向时,由于某种原因,我陷入了无限循环重定向.是否有解决方案可在每个页面上创建登录表单,而不是重定向到另一个视图,而只需使用Blade的模板引擎刷新页面并设置正确的值即可? 我已经使用php artisan make:auth命令创建了基础,并且自己进行了一些更改.我将贴出相应类的摘要.

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.

大致了解它的外观&为什么每个页面上都需要一个表单:(顺便说一句,我将其放在 layouts/app.blade.php 中的页脚中.

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.

Routes/web.php: 我对此取消了Facade Auth :: routes(),以便进行更改.我暂时退出了登录名,但是当我的工作正常时,登录名应该消失了.我忽略了最下面的2条评论.

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

HomeController.php

在这里,我怀疑构造函数中的配置是作为中间件作为"guest"还是"auth"?

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(与工匠身份验证没有区别):

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:

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:

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.

在您的web.php路由文件中,您两次声明了本地路由:

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');

我将其更改为:

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

然后在您的LoginController中,您需要更新$redirectTo以反映该更改.所以:

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

protected $redirectTo = '/';

与您的RedirectIfAuthenticated中间件相同:

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

最后但并非最不重要的是,您的HomeController中间件不正确. guest用于仅在未经身份验证的情况下才能访问的路由,例如登录,注册或忘记密码.但是,由于您希望仅通过身份验证的用户才能访问HomeController中的路由,因此您必须将中间件更改为auth:

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');
}