【发布时间】:2016-03-09 02:52:30
【问题描述】:
我在服务提供商中有以下内容:
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);
【问题讨论】:
-
您是否有某种 ajax 中间件出现故障或重定向到其他地方?没有理由仅仅因为请求是通过 ajax 发出的而不运行。