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 cgictest.cgi文件无法访问
  • ¥20 删除和修改功能无法调用
  • ¥15 kafka topic 所有分副本数修改
  • ¥15 小程序中fit格式等运动数据文件怎样实现可视化?(包含心率信息))
  • ¥15 如何利用mmdetection3d中的get_flops.py文件计算fcos3d方法的flops?
  • ¥40 串口调试助手打开串口后,keil5的代码就停止了
  • ¥15 电脑最近经常蓝屏,求大家看看哪的问题
  • ¥60 高价有偿求java辅导。工程量较大,价格你定,联系确定辅导后将采纳你的答案。希望能给出完整详细代码,并能解释回答我关于代码的疑问疑问,代码要求如下,联系我会发文档
  • ¥50 C++五子棋AI程序编写
  • ¥30 求安卓设备利用一个typeC接口,同时实现向pc一边投屏一边上传数据的解决方案。