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 file converter 转换格式失败 报错 Error marking filters as finished,如何解决?
  • ¥15 ubuntu系统下挂载磁盘上执行./提示权限不够
  • ¥15 Arcgis相交分析无法绘制一个或多个图形
  • ¥15 关于#r语言#的问题:差异分析前数据准备,报错Error in data[, sampleName1] : subscript out of bounds请问怎么解决呀以下是全部代码:
  • ¥15 seatunnel-web使用SQL组件时候后台报错,无法找到表格
  • ¥15 fpga自动售货机数码管(相关搜索:数字时钟)
  • ¥15 用前端向数据库插入数据,通过debug发现数据能走到后端,但是放行之后就会提示错误
  • ¥30 3天&7天&&15天&销量如何统计同一行
  • ¥30 帮我写一段可以读取LD2450数据并计算距离的Arduino代码
  • ¥15 飞机曲面部件如机翼,壁板等具体的孔位模型