Php 登录后访问Laravel 5.8中的子页面
我学习Laravel已经几天了,发现了以下问题。 在我的应用程序中,我希望有3个访问级别:Php 登录后访问Laravel 5.8中的子页面,php,laravel,laravel-5,Php,Laravel,Laravel 5,我学习Laravel已经几天了,发现了以下问题。 在我的应用程序中,我希望有3个访问级别: 用户未登录 用户已登录(角色:user和userPremium) 用户管理员(角色:管理员) 在我的项目中,我使用Laravel内置的注册和日志功能 我有以下迁移: Schema::create('users', function (Blueprint $table) { $table->bigIncrements('id'); $table->
Schema::create('users', function (Blueprint $table) {
$table->bigIncrements('id');
$table->bigInteger('company_id')->unsigned();
$table->foreign('company_id')->references('id')->on('companies')->onDelete('cascade');
$table->boolean('enable')->default(0);
$table->string('name', 120)->nullable();
$table->string('surname', 120)->nullable();
$table->string('email', 120)->unique();
$table->timestamp('email_verified_at')->nullable();
$table->string('password');
$table->bigInteger('counter')->default(0);
$table->string('url_address', 160);
$table->boolean('isCompany')->default(0);
$table->boolean('isMailing')->default(0);
$table->text('content')->nullable();
$table->string('nip1', 12)->nullable();
$table->string('business1', 120)->nullable();
$table->string('phone1', 60)->nullable();
$table->string('street1', 150)->nullable();
$table->string('number1', 8)->nullable();
$table->string('postal_code1', 12)->nullable();
$table->string('city1', 100)->nullable();
$table->bigInteger('country_id1')->default(0);
$table->bigInteger('provincial_id1')->default(0);
$table->string('nip2', 12)->nullable();
$table->string('business2', 120)->nullable();
$table->string('phone2', 60)->nullable();
$table->string('street2', 150)->nullable();
$table->string('number2', 8)->nullable();
$table->string('postal_code2', 12)->nullable();
$table->string('city2', 100)->nullable();
$table->bigInteger('country_id2')->default(0);
$table->bigInteger('provincial_id2')->default(0);
$table->string('nip3', 12)->nullable();
$table->string('business3', 120)->nullable();
$table->string('phone3', 60)->nullable();
$table->string('street3', 150)->nullable();
$table->string('number3', 8)->nullable();
$table->string('postal_code3', 12)->nullable();
$table->string('city3', 100)->nullable();
$table->bigInteger('country_id3')->default(0);
$table->bigInteger('provincial_id3')->default(0);
$table->decimal('cash', 9, 2)->default(0);
$table->decimal('lng', 10, 8)->default(0);
$table->decimal('lat', 10, 8)->default(0);
$table->boolean('enable_map')->default(0);
$table->rememberToken();
$table->timestamps();
$table->engine = "InnoDB";
});
Schema::create('roles', function (Blueprint $table) {
$table->bigIncrements('id');
$table->string('name');
$table->engine = "InnoDB";
});
Schema::create('role_user', function (Blueprint $table) {
$table->bigInteger('user_id')->unsigned();
$table->foreign('user_id')->references('id')->on('users')->onDelete('cascade');
$table->bigInteger('role_id')->unsigned();
$table->foreign('role_id')->references('id')->on('roles')->onDelete('cascade');
$table->engine = "InnoDB";
});
DB::table('roles')->insert([
'name' => $faker->unique()->randomElement(['admin', 'user', 'userPremium']),
]);
我的路由器如下所示(web.php):
User.php模型如下所示:
class User extends Authenticatable implements MustVerifyEmail
{
use Notifiable;
use psCMS\Presenters\UserPresenter;
public static $roles = [];
/**
* The attributes that are mass assignable.
*
* @var array
*/
protected $fillable = ['company_id', 'enable', 'name', 'surname', 'email', 'email_verified_at', 'password', 'counter', 'url_address', 'isCompany', 'isMailing', 'content', 'nip1', 'business1', 'phone1', 'street1', 'number1', 'postal_code1', 'city1', 'country_id1', 'provincial_id1', 'nip2', 'business2', 'phone2', 'street2', 'number2', 'postal_code2', 'city2', 'country_id2', 'provincial_id2', 'nip3', 'business3', 'phone3', 'street3', 'number3', 'postal_code3', 'city3', 'country_id3', 'provincial_id3', 'cash', 'lng', 'lat', 'enable_map', 'remember_token', 'created_at', 'updated_at', 'last_login_at', 'last_login_ip' ];
/**
* The attributes that should be hidden for arrays.
*
* @var array
*/
protected $hidden = [
'password', 'remember_token',
];
public function photos()
{
return $this->morphMany('App\Photo', 'photoable');
}
public function roles()
{
return $this->belongsToMany('App\Role');
}
public function hasRole(array $roles)
{
foreach($roles as $role)
{
if(isset(self::$roles[$role]))
{
if(self::$roles[$role]) return true;
}
else
{
self::$roles[$role] = $this->roles()->where('name', $role)->exists();
if(self::$roles[$role]) return true;
}
}
return false;
}
}
我该怎么做
我怎样才能做到呢?您需要编写一个中间件。在此中间件中,检查用户是否经过身份验证或检查用户角色以进行有效重定向。请阅读文档。我希望它会有所帮助。您可以创建AdminMiddleware并使用它检查用户是否为admin
class User extends Authenticatable implements MustVerifyEmail
{
use Notifiable;
use psCMS\Presenters\UserPresenter;
public static $roles = [];
/**
* The attributes that are mass assignable.
*
* @var array
*/
protected $fillable = ['company_id', 'enable', 'name', 'surname', 'email', 'email_verified_at', 'password', 'counter', 'url_address', 'isCompany', 'isMailing', 'content', 'nip1', 'business1', 'phone1', 'street1', 'number1', 'postal_code1', 'city1', 'country_id1', 'provincial_id1', 'nip2', 'business2', 'phone2', 'street2', 'number2', 'postal_code2', 'city2', 'country_id2', 'provincial_id2', 'nip3', 'business3', 'phone3', 'street3', 'number3', 'postal_code3', 'city3', 'country_id3', 'provincial_id3', 'cash', 'lng', 'lat', 'enable_map', 'remember_token', 'created_at', 'updated_at', 'last_login_at', 'last_login_ip' ];
/**
* The attributes that should be hidden for arrays.
*
* @var array
*/
protected $hidden = [
'password', 'remember_token',
];
public function photos()
{
return $this->morphMany('App\Photo', 'photoable');
}
public function roles()
{
return $this->belongsToMany('App\Role');
}
public function hasRole(array $roles)
{
foreach($roles as $role)
{
if(isset(self::$roles[$role]))
{
if(self::$roles[$role]) return true;
}
else
{
self::$roles[$role] = $this->roles()->where('name', $role)->exists();
if(self::$roles[$role]) return true;
}
}
return false;
}
}