【问题标题】:Laravel 5.2 with Entrust - Class name must be a valid object or a string带有 Entrust 的 Laravel 5.2 - 类名必须是有效的对象或字符串
【发布时间】:2016-06-17 13:29:17
【问题描述】:

注册用户时遇到问题,在我的表用户中保存用户后,我尝试为该用户分配角色,但收到错误:

类名必须是有效的对象或字符串。

我的代码是

(App\Http\Controllers\auth\AuthController.php)

<?php

namespace App\Http\Controllers\Auth;

use App\User;
use App\Role;

use Validator;
use App\Http\Controllers\Controller;
use App\Http\Controllers\Controller\Auth;
use Illuminate\Foundation\Auth\ThrottlesLogins;
use Illuminate\Foundation\Auth\AuthenticatesAndRegistersUsers;
use Illuminate\Http\Request;


class AuthController extends Controller
{

    public function postRegister(Request $request){

            $this->validate($request,[
                'name' => 'required|min:4|max:255|unique:users',
                'email'=>'required|email|max:255|unique:users',
                'password'=>'required|confirmed|min:3'
                ]);

            $user_data = array(
               //$field => $request->input('login'),
               'name'=> $request->input('name'),
               'email' => $request->input('email'),
               'password' => $request->input('password')
            );

            $user=User::create([
                    'name'=>$user_data['name'],
                    'email'=>$user_data['email'],
                    'password'=>bcrypt($user_data['password']),
                    'active'=>1                
                ]);

            echo $user;
            $role = Role::where('name','=','admin')->first();

            //$user->attachRole($role->id);            
            $user->roles()->attach($role->id);

            //return redirect('auth/register')->with('message','store');

        }
}

$user 上的 echo 打印:

{"name":"bbbbbvq","email":"bb@bbv.comq","active":1,"updated_at":"2016-03-03 19:07:24","created_at":"2016-03-03 19:07:24","id":32}

我将委托Zizaco\Entrust\src\config\config.php复制到我的proyect\app\config\entrust.php,并用这种方法修改了文件test\vendor\zizaco\entrust\src\Entrust\EntrustServiceProvider.php

  private function registerCommands()
    {
        /*
        $this->app->bindShared('command.entrust.migration', function ($app) {
            return new MigrationCommand();
        });
        $this->app->bindShared('command.entrust.classes', function ($app) {
            return new ClassCreatorCommand();
        });*/

        $this->app->singleton('command.entrust.migration', function ($app) {
            return new MigrationCommand();
        });
        $this->app->singleton('command.entrust.classes', function ($app) {
            return new ClassCreatorCommand();
        });
    }

【问题讨论】:

  • 您的错误应该告诉您错误发生的位置(文件名和行号)。那真的很有帮助。

标签: php laravel user-roles entrust


【解决方案1】:

这为我解决了这个问题。

第 51 行的vendor/zizaco/entrust/src/Entrust/Traits/EntrustRoleTrait.php 将调用Config::get('auth.model') 作为$this-&gt;belongsToMany( 方法调用的第一个参数。

public function users()
{
    return $this->belongsToMany(Config::get('auth.model'), ...
    // return $this->belongsToMany(Config::get('auth.model'), ...
}

您可以将其更改为 Config::get('auth.providers.users.model') 或更新您的 config/auth.php 文件以包含条目 model =&gt; App\Users::class

'model' => App\Users::class,  

'providers' => [
    'users' => [
        'driver' => 'eloquent',
        'model' => App\Users::class,
    ],
],

我的首选是更新 config/auth.php 文件,因为对供应商文件夹的任何更改都不会提供给您团队中的其他人,或者当您转移到生产环境时。

当然,如果您为您的用户使用不同的模型,您可以提供它。

【讨论】:

  • 适用于 5.1
【解决方案2】:

Entrust 还没有升级到 5.2,所以你需要稍微摆弄一下。

作为tapos gosh has said before,您需要进入vendor/zizaco/entrust/src/commands/MigrationCommand.php 和第86 行:

删除

$usersTable  = Config::get('auth.table');
$userModel   = Config::get('auth.model');

然后替换为

$usersTable  = Config::get('auth.providers.users.table');
$userModel   = Config::get('auth.providers.users.model');

然后在config/auth.php 文件中像这样写提供者行:

'providers' => [
    'users' => [
        'driver' => 'eloquent',
        'model' => App\User::class,
        'table' => 'users',
    ],

    // 'users' => [
    //     'driver' => 'database',
    //     'table' => 'users',
    // ],
],

【讨论】:

  • 感谢您的回答,但我做了您提到的操作,但仍然出现同样的错误。
  • @Jose 尝试运行 php artisan config:clear 以刷新您的配置设置。
  • 这并没有解决我的问题,尽管我确实注意到您建议的一些更改现在是 github 包的一部分。
【解决方案3】:

我在 assignRole() 给用户时遇到了同样的问题。解决办法是roleguard_name必须是'

网络

'。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2023-01-13
    • 1970-01-01
    • 1970-01-01
    • 2017-08-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多