服务提供商没有';t在ajax上运行


Service provider doesn't run on ajax

我在服务提供商中有以下内容:

protected $defer = false;
public function register() {
    $this->app->singleton('User', function ($app) {
        $user = new User();
        $user->setUser(Auth::user() ? Auth::user() : null);
        dd($user);
        return $user;
    });
}

正如您所看到的,我有一个dd()调用,每当我加载页面时,它都会转储传递给它的数据

我在页面上有一个ajax请求,当我发出ajax请求时,dd()甚至不会运行。

因此:

a( 服务提供商在什么时候运行?

b( 我能做些什么来解决我的问题?

编辑

我创建了一个这样的新用户:

public function UserService() {
    $this->app->singleton('User', function ($app) {
        return new User();
    });
}

然后在我的用户服务中,我有这个:

public function __construct(UserModel $user = null){
    if($user === null){
        $this->user = Auth::user();
    }else{
        $this->user = $user;
    }
}
public function getUserId(){
    dd($this->user);
    return isset($this->user->id) ? $this->user->id : null;
}
public function getAcesseId(){
    return isset($this->user->acesse_id) ? $this->user->acesse_id : null;
}

以下是页面加载的结果:

UserModel {#261
  #rules: array:1 [
    "player_tag" => "required|min:3|max:15|regex:/^(?=.*[a-zA-Z]).+$/|regex:/^[a-zA-Z0-9_]+$/"
  ]
  #table: "users"
  #fillable: array:1 [
    0 => "player_tag"
  ]
  #hidden: array:1 [
    0 => "remember_token"
  ]
  #errors: null
  #connection: null
  #primaryKey: "id"
  #perPage: 15
  +incrementing: true
  +timestamps: true
  #attributes: array:6 [
    "id" => 1
    "acesse_id" => 108649
    "player_tag" => "TheColorRed"
    "remember_token" => null
    "created_at" => "2016-03-08 11:31:02"
    "updated_at" => "2016-03-08 11:31:02"
  ]
  #original: array:6 [
    "id" => 1
    "acesse_id" => 108649
    "player_tag" => "TheColorRed"
    "remember_token" => null
    "created_at" => "2016-03-08 11:31:02"
    "updated_at" => "2016-03-08 11:31:02"
  ]
  #relations: []
  #visible: []
  #appends: []
  #guarded: array:1 [
    0 => "*"
  ]
  #dates: []
  #dateFormat: null
  #casts: []
  #touches: []
  #observables: []
  #with: []
  #morphClass: null
  +exists: true
  +wasRecentlyCreated: false
}

下面是来自同一页面的ajax请求的结果:

UserModel {#254
  #rules: array:1 [
    "player_tag" => "required|min:3|max:15|regex:/^(?=.*[a-zA-Z]).+$/|regex:/^[a-zA-Z0-9_]+$/"
  ]
  #table: "users"
  #fillable: array:1 [
    0 => "player_tag"
  ]
  #hidden: array:1 [
    0 => "remember_token"
  ]
  #errors: null
  #connection: null
  #primaryKey: "id"
  #perPage: 15
  +incrementing: true
  +timestamps: true
  #attributes: []
  #original: []
  #relations: []
  #visible: []
  #appends: []
  #guarded: array:1 [
    0 => "*"
  ]
  #dates: []
  #dateFormat: null
  #casts: []
  #touches: []
  #observables: []
  #with: []
  #morphClass: null
  +exists: false
  +wasRecentlyCreated: false
}

由于某些原因,转储中没有包含正确的信息。用户属性在哪里?

这是AJAX请求的控制器:

use App'Services'User;
public function UploadAvatar(User $user){
    $userid = $user->getAcesseId();
    if($userid > 0){
        $md5  = md5($userid);
        $disk = Storage::disk('avatars');
        $file = $md5 . '.png';
        $image = new Imagick();
        $image->readImageBlob(file_get_contents($_FILES['file']['tmp_name']));
        $image->setImageFormat('png');
        $disk->getDriver()->put('tmp/' . $file, $image->getImageBlob(), [
            'visibility' => 'public'
        ]);
        return response()->json(env('URL_AVATARS_TMP') . $file);
    }
    return response()->json(['error' => 'Invalid User'], 500);
}

下面一行是返回的内容:

return response()->json(['error' => 'Invalid User'], 500);

如果您的服务提供商被添加到config/app.php中的providers列表中,那么它将在内核的引导过程中自动加载到每个请求上,该过程在处理请求之前执行。因此,register方法对每个请求都执行,不管它是否是AJAX请求。

另一方面,singleton闭包中的代码只会在您第一次需要singleton的实例时执行,因此当您通过容器创建实例时:

$user = app('User');

然后,也只有到那时,才会创建singleton实例并执行dd函数调用,在注册绑定时,默认情况下不会创建它。所以我猜测您的AJAX调用没有执行任何请求该实例的代码。


话虽如此,根据您的单例代码,我假设您正在尝试缓存已登录的User模型实例(可能是为了避免在同一请求期间从多个位置访问用户详细信息时对数据库进行额外查询(。

如果是这种情况,那么它是多余的,因为Laravel的Illuminate'Auth'SessionGuard类在首次从数据库获取详细信息后已经在缓存用户实例。使用Laravel DebugBar这样的包会发现,无论在请求期间调用Auth::user()多少次,每个请求都只运行一个查询来获取经过身份验证的用户详细信息。