我的路线文件就像
Route::group(['middleware' => ['web']], function () {
Route::get('/', function () {
return view('login');
});
Route::auth();
Route::get('/home', 'HomeController@index');
});
答案 0 :(得分:2)
解决方案#1(任何经过身份验证的用户)
在档案/app/Http/Controllers/Auth/RegisterController.php
只需从
更改即可public function __construct()
{
$this->middleware('guest');
}
到
public function __construct()
{
$this->middleware('auth');
}
解决方案#2(仅经过身份验证的管理员用户) a)运行
php artisan make:middleware IsAdmin
b)通过打开app / Http / Kernel.php添加到内核文件中的routeMiddleware数组:
'admin' => \App\Http\Middleware\IsAdmin::class,
c)编辑isAdmin文件
public function handle($request, Closure $next)
{
if (Auth::user() && Auth::user()->is_admin == 1) {
return $next($request);
}
return redirect('/');
}
d)在文件/app/Http/Controllers/Auth/RegisterController.php上,从
更改构造函数public function __construct()
{
$this->middleware('guest');
}
到
public function __construct()
{
$this->middleware('admin');
}
e)更新用户迁移以包括de boolean field
Schema::create('users', function (Blueprint $table) {
$table->mediumIncrements('id');
$table->string('name');
$table->string('email')->unique();
$table->string('password');
$table->boolean('is_admin')->default(false);
$table->rememberToken();
$table->timestamps();
});
f)更新用户模型以允许'is_admin'字段
protected $fillable = [
'name', 'email', 'password', 'is_admin'
];
答案 1 :(得分:0)
Route类中的静态方法auth()正在为您生成路由,因此您只需删除以下行:
Route::auth();
并手动放置您想要启用的路线,在您的情况下只需登录:
Route::get('auth/login', 'Auth\AuthController@getLogin');
Route::post('auth/login', 'Auth\AuthController@postLogin');
Route::get('auth/logout', 'Auth\AuthController@getLogout');
希望它有所帮助!