简体   繁体   English

找不到类“角色” [Symfony \\ Component \\ Debug \\ Exception \\ FatalErrorException]

[英]Class 'Role" not found [Symfony\Component\Debug\Exception\FatalErrorException]

So I've seen a lot of similar questions asked about class not found errors but unless I'm totally missing somethings obvious, I just can't understand how the following method call does not see my role class and results in the class not found exception: 因此,我已经看到很多关于类未找到错误的类似问题,但是除非我完全缺少明显的东西,否则我将无法理解以下方法调用如何看不到我的角色类以及导致该类未找到的结果例外:

 $user->makeEmployee("admin")

Here is my user class with makeEmployee(): 这是我的带有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);
}
}

And here is my Role class: 这是我的角色类:

<?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');
    }
}

If they are both is the same namespace can't I just use Role in User ? 如果它们都是相同的名称空间,我不能只使用User Role吗? I've also tried clear-compile and composer dump-auto as well as replacing the Role reference with App\\Role and including use App\\Role at the top. 我还尝试了clear-compile和composer dump-auto以及将Role引用替换为App\\Role并在顶部use App\\Role I'm using PHPStorm and it catches the reference fine as well as I am able to jump to the Role class definition from the user code. 我正在使用PHPStorm,它可以很好地引用参考,也可以从用户代码跳转到Role类定义。 Thanks in advance for your help! 在此先感谢您的帮助!

您无需传递类, Role是要在要扩展的类中调用的方法的字符串参数,因此您需要提供该类及其完整的名称空间。

return $this->belongsToMany('App\Role', 'users_roles');

you should be doing 你应该在做

return $this->belongsToMany('App\Role', 'users_roles');

and dump-auto and dump-autoload both are same. 和dump-auto和dump-autoload都相同。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Symfony \\\\ Component \\\\ Debug \\\\ Exception \\\\ FatalErrorException“,” message“:”找不到类&#39;Authy_Api&#39; - Symfony\\Component\\Debug\\Exception\\FatalErrorException“,”message“:”Class 'Authy_Api' not found 在 Laravel 项目中找不到 Symfony\\Component\\Debug\\Exception\\FatalErrorException Trait - Symfony\Component\Debug\Exception\FatalErrorException Trait not found in Laravel project FatalErrorException:错误:未找到类&#39;Symfony \\ Component \\ Form \\ Exception \\ FormException&#39; - FatalErrorException: Error: Class 'Symfony\Component\Form\Exception\FormException' not found Symfony\Component\Debug\Exception\FatalErrorException laravel 错误 - Symfony\Component\Debug\Exception\FatalErrorException laravel Error 如何在 PHP (Laravel) 中调试“Symfony\\Component\\Debug\\Exception\\FatalErrorException”错误? - How to debug "Symfony\Component\Debug\Exception\FatalErrorException" errors in PHP (Laravel)? 找不到Symfony \\ Component \\ Debug \\ Exception \\ FatalThrowableError类&#39;\\ App \\ Admin&#39; - Symfony\Component\Debug\Exception\FatalThrowableError Class '\App\Admin' not found Symfony \ Component \ Debug \ Exception \ FatalThrowableError类'Buno \ Readr \ ReadrServiceProvider'未找到 - Symfony\Component\Debug\Exception\FatalThrowableError Class 'Buno\Readr\ReadrServiceProvider' not found 找不到Laravel Forge返回[Symfony \\ Component \\ Debug \\ Exception \\ FatalThrowableError]类 - Laravel Forge returning [Symfony\Component\Debug\Exception\FatalThrowableError] class not found Symfony \\ Component \\ Debug \\ Exception \\ FatalErrorException调用未定义的方法Illuminate \\ Database \\ MySqlConnection :: setReconnector() - Symfony \ Component \ Debug \ Exception \ FatalErrorException Call to undefined method Illuminate\Database\MySqlConnection::setReconnector() FatalErrorException:错误:在symfony2上找不到类&#39;PDO&#39; - FatalErrorException: Error: Class 'PDO' not found on symfony2
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM