[英]Symfony\\Component\\Debug\\Exception\\FatalErrorException“,”message“:”Class 'Authy_Api' not found
[英]Class 'Role" not found [Symfony\Component\Debug\Exception\FatalErrorException]
因此,我已經看到很多關於類未找到錯誤的類似問題,但是除非我完全缺少明顯的東西,否則我將無法理解以下方法調用如何看不到我的角色類以及導致該類未找到的結果例外:
$user->makeEmployee("admin")
這是我的帶有makeEmployee()的用戶類:
<?php namespace App;
use Illuminate\Auth\Authenticatable;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Auth\Passwords\CanResetPassword;
use Illuminate\Contracts\Auth\Authenticatable as AuthenticatableContract;
use Illuminate\Contracts\Auth\CanResetPassword as CanResetPasswordContract;
class User extends Model implements AuthenticatableContract, CanResetPasswordContract {
use Authenticatable, CanResetPassword;
/**
* The database table used by the model.
*
* @var string
*/
protected $table = 'users';
/**
* The attributes that are mass assignable.
*
* @var array
*/
protected $fillable = ['name', 'email', 'password'];
/**
* The attributes excluded from the model's JSON form.
*
* @var array
*/
protected $hidden = ['password', 'remember_token'];
/**
* Get the roles a user has
*/
public function roles()
{
return $this->belongsToMany('Role', 'users_roles');
}
/**
* Find out if User is an employee, based on if has any roles
*
* @return boolean
*/
public function isEmployee()
{
$roles = $this->roles->toArray();
return !empty($roles);
}
/**
* Find out if user has a specific role
*
* $return boolean
*/
public function hasRole($check)
{
return in_array($check, array_fetch($this->roles->toArray(), 'name'));
}
/**
* Get key in array with corresponding value
*
* @return int
*/
private function getIdInArray($array, $term)
{
foreach ($array as $key => $value) {
if ($value == $term) {
return $key;
}
}
throw new UnexpectedValueException;
}
/**
* Add roles to user to make them a concierge
*/
public function makeEmployee($title)
{
$assigned_roles = array();
$roles = array_fetch(Role::all()->toArray(), 'name');
switch ($title) {
case 'admin':
$assigned_roles[] = $this->getIdInArray($roles, 'create_message');
/*case 'member':
$assigned_roles[] = $this->getIdInArray($roles, 'create_customer');
case 'concierge':
$assigned_roles[] = $this->getIdInArray($roles, 'add_points');
$assigned_roles[] = $this->getIdInArray($roles, 'redeem_points');*/
break;
default:
throw new \Exception("The employee status entered does not exist");
}
$this->roles()->attach($assigned_roles);
}
}
這是我的角色類:
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Role extends Model
{
protected $table = 'roles';
/**
* Set timestamps off
*/
public $timestamps = false;
/**
* Get users with a certain role
*/
public function users()
{
return $this->belongsToMany('User', 'users_roles');
}
}
如果它們都是相同的名稱空間,我不能只使用User
Role
嗎? 我還嘗試了clear-compile和composer dump-auto以及將Role
引用替換為App\\Role
並在頂部use App\\Role
。 我正在使用PHPStorm,它可以很好地引用參考,也可以從用戶代碼跳轉到Role
類定義。 在此先感謝您的幫助!
您無需傳遞類, Role
是要在要擴展的類中調用的方法的字符串參數,因此您需要提供該類及其完整的名稱空間。
return $this->belongsToMany('App\Role', 'users_roles');
你應該在做
return $this->belongsToMany('App\Role', 'users_roles');
和dump-auto和dump-autoload都相同。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.