带有委托的 Laravel 5.2 - 类名必须是有效的对象或字符串



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

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

我的代码是

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

<?php
namespace AppHttpControllersAuth;
use AppUser;
use AppRole;
use Validator;
use AppHttpControllersController;
use AppHttpControllersControllerAuth;
use IlluminateFoundationAuthThrottlesLogins;
use IlluminateFoundationAuthAuthenticatesAndRegistersUsers;
use IlluminateHttpRequest;

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}

我将委托ZizacoEntrustsrcconfigconfig.php复制到我的proyectappconfigentrust.php,并在此方法中修改了文件testvendorzizacoentrustsrcEntrustEntrustServiceProvider.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();
        });
    }

这为我解决了这个问题。

第 51 行的vendor/zizaco/entrust/src/Entrust/Traits/EntrustRoleTrait.php将调用Config::get('auth.model')作为$this->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 => AppUsers::class

'model' => AppUsers::class,  
'providers' => [
    'users' => [
        'driver' => 'eloquent',
        'model' => AppUsers::class,
    ],
],

我倾向于更新config/auth.php文件,因为对供应商文件夹的任何更改都不适用于团队中的其他人,或者当您移动到生产环境时。

当然,如果您为用户使用不同的模型,您将改为提供该模型。

Entrust 尚未升级到 5.2,因此您需要稍微摆弄一下。

正如tapos gosh之前所说,你需要进入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' => AppUser::class,
        'table' => 'users',
    ],
    // 'users' => [
    //     'driver' => 'database',
    //     'table' => 'users',
    // ],
],

我对用户的assignRole()也面临着同样的问题。解决方案是角色guard_name必须为"

'。

相关内容

  • 没有找到相关文章

最新更新