我已经尝试了许多方法,但是没有尝试过如何不能正确路由和重定向。我希望,如果有管理员进来,他们会重定向到仪表板,如果只是用户,则将其重定向到主页。我没有关于如何执行此操作的想法。请帮助我
role_user_table
public function up()
{
Schema::create('role_user', function (Blueprint $table) {
$table->integer('role_id');
$table->integer('user_id');
$table->foreign('role_id')->references('id')->on('roles')->onDelete('cascade');
$table->foreign('user_id')->references('id')->on('users')->onDelete('cascade');
$table->primary(['user_id', 'role_id']);
});
}
roles_table 1.管理员 2.注册用户
public function up()
{
Schema::create('roles', function (Blueprint $table) {
$table->increments('id');
$table->string('name');
});
}
用户表
Schema::create('users', function (Blueprint $table) {
$table->increments('id');
$table->tinyInteger('role_id')->unsigned()->nullable();
$table->string('name');
$table->string('email');
$table->string('password');
$table->rememberToken();
$table->timestamps();
$table->foreign('role_id')->references('id')->on('roles');
});
中间件
namespace App\Http\Middleware;
use Closure;
class CheckIfAdmin{
public function handle($request, Closure $next)
{
$user = $request->user();
if (!isset($user)) {
return redirect('admin/login');
}
if (!$user->isAdmin()) {
return redirect('/');
}
return $next($request);
}
}
Kernel.php
protected $routeMiddleware = [
'auth' => \Illuminate\Auth\Middleware\Authenticate::class,
'auth.basic' => \Illuminate\Auth\Middleware\AuthenticateWithBasicAuth::class,
'bindings' => \Illuminate\Routing\Middleware\SubstituteBindings::class,
'can' => \Illuminate\Auth\Middleware\Authorize::class,
'guest' => \App\Http\Middleware\RedirectIfAuthenticated::class,
'throttle' => \Illuminate\Routing\Middleware\ThrottleRequests::class,
'admin' => \App\Http\Middleware\CheckIfAdmin::class,
];
Role.php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Role extends Model
{
protected $fillable = ['name'];
public $timestamps = false;
public function users()
{
return $this->hasMany(User::class);
}
}
User.php
namespace App;
use Illuminate\Notifications\Notifiable;
use Illuminate\Foundation\Auth\User as Authenticatable;
use App\Role;
class User extends Authenticatable
{
use Notifiable;
protected $fillable = [
'name', 'email', 'password',
];
protected $hidden = [
'password', 'remember_token',
];
public function roles()
{
return $this->belongsToMany(Role::class);
}
public function isAdmin()
{
return (boolean)$this->roles->where('name', 'admin')->count();
}
}
web.php
Route::group(['prefix'=>'admin', 'namespace'=>'Admin', 'middleware'=>
['auth']], function(){
Route::resource('/category', 'CategoryController', ['as'=>'admin']);
Route::resource('/article', 'ArticleController', ['as'=>'admin']);
Route::group(['prefix' => 'user_managment', 'namespace' =>
'UserManagment'], function() {
Route::resource('/user', 'UserController', ['as' =>
'admin.user_managment']);
});
Route::group(['middleware' => 'admin'], function() {
Route::get('/', 'DashboardController@dashboard')-
>name('admin.index');
});
});
Route::get('/', function () {
return view('blog.home');
});
Auth::routes();
Route::get('/home', 'HomeController@index')->name('home');
现在,如果管理员已在url admin行中注册,他将成为管理员,如果一个简单的用户这样做,他将不会错过,但我需要他自动将其扔在仪表板上。仪表板位于admin文件夹中
答案 0 :(得分:0)
由于您已经编写了大部分代码,因此应该可以:
Route::group(['prefix'=>'admin', 'namespace'=>'Admin', 'middleware'=>['admin']], function(){
Route::get('/', 'DashboardController@dashboard')->name('admin.index');
Route::resource('/category', 'CategoryController', ['as'=>'admin']);
Route::resource('/article', 'ArticleController', ['as'=>'admin']);
Route::group(['prefix' => 'user_managment', 'namespace' => 'UserManagment'], function() {
Route::resource('/user', 'UserController', ['as' => 'admin.user_managment']);
});
});
Route::get('/', function () {
return view('blog.home');
}); // Not sure if you want this behind the middleware or not
Auth::routes();
Route::get('/home', 'HomeController@index')->name('home');
我将在您的中间件中更改此行:
if (!$user->isAdmin()) {
return redirect('/home');
}
根据您的评论将非管理员使用重定向到/home
。这里不需要使用Auth
和admin
中间件,因为admin
可以处理非管理员用户。它应该允许所有管理员访问admin
中间件中包装的组,并将非管理员重定向到/home
路由。让我知道这是您要找的内容,还是我可以为您优化此内容。
对于重定向问题:
签出:https://codeburst.io/learn-how-to-redirect-authenticated-users-to-corresponding-path-in-laravel-dd613e2f9e3您可以将protected $redirectTo = 'admin';
更改为功能:public function redirectTo(){}
。最终会像:
public function redirectTo(){
// User role
$role = Auth::user()->role->pluck('name')->toArray();
if(in_array('admin', $role)){
return '/admin/login';
}else{
return '/home';
}
}