Laravel 5.2 身份验证仅通过登录用户或仅由管理员注册用户


Laravel 5.2 Auth register users only by logged user or by admin only

我对拉拉维尔路线有问题。我希望只有管理员或登录用户才能访问该身份验证注册路由。为了实现这一点,我从路由中删除了Route::auth();.php并在身份验证中间件中创建自己的路由条目。

努力

Route::group(['middleware' => 'web'], function () {
    // Authentication Routes...
    $this->get('login', 'Auth'AuthController@showLoginForm');
    $this->post('login', 'Auth'AuthController@login');
    $this->get('logout', 'Auth'AuthController@logout');
    // Password Reset Routes...
    $this->get('password/reset/{token?}', 'Auth'PasswordController@showResetForm');
    $this->post('password/email', 'Auth'PasswordController@sendResetLinkEmail');
    $this->post('password/reset', 'Auth'PasswordController@reset');
    Route::get('/home', 'HomeController@index');
    Route::post('/ajax/getStates', 'ConfigurationController@getStates');
    Route::post('/ajax/getCities', 'ConfigurationController@getCities');

});

Route::group(['middleware' => ['web','auth']], function () {
    // Registration Routes...
    $this->get('register', 'Auth'AuthController@showRegistrationForm');
    $this->post('register', 'Auth'AuthController@register');
});

上面的代码工作正常。 当我尝试访问注册 URL 时,它只是将我重定向到登录页面。现在实际问题在登录后开始。

登录后,我尝试访问注册页面,但它没有显示,而是像http://localhost/一样将我重定向到主页。

请向我建议解决方案。

检查 AuthController 的构造函数。它将来宾中间件分配给除logout以外的所有方法。

我得到了一个简单的解决方案。我不是专家,但我以这种方式进行,它正在按照您想要的方式工作。

Route::group(['middleware' => 'web'], function () {
Route::auth();
Route::get('/edit', function () {
    if (Auth::user()) {
        return view('auth.user_edit'); //Page which you want to show for loged user.
    } else {
        return "MM"; //You can redirect from here, if user is not logged in
    }
});
});