Laravel DI在立面基础类中使用时不起作用 [英] Laravel DI not working when using in Facades Underlying Class
问题描述
这是我的外墙课程
<?php
namespace App\Facades;
use Illuminate\Support\Facades\Facade;
class UserFacade extends Facade
{
protected static function getFacadeAccessor()
{
return 'user';
}
}
这就是我绑定IOC的方式(不确定我是否使用正确的术语)
This is how i bind into IOC (Not sure if i am using correct terms)
<?php
namespace App\Providers;
use Illuminate\Support\ServiceProvider;
use Illuminate\Support\Facades\App;
class RUserServiceProvider extends ServiceProvider
{
/**
* Register the application services.
*
* @return void
*/
public function register()
{
App::bind('user', function() {
return new \App\Http\Responses\RUser;
});
}
}
这是底层类,其中包含用户实体的实际代码
This is the Underlying class that contains actual code for user entity
<?php
namespace App\Http\Responses;
use Illuminate\Database\Eloquent\ModelNotFoundException;
use App\Utils\TokenGenerator;
use App\Models\User;
use Validator;
use Hash;
class RUser
{
protected $token_generator;
public function __construct(TokenGenerator $token_generator)
{
$this->token_generator = $token_generator;
}
/**
* Signup User
* @param Request $request
* @return array $response
*/
public function signup($data)
{
$response = [];
$access_token = $this->token_generator->generate(32);
$user = User::create([
'email' => $data->email,
'password' => Hash::make($data->password),
'company_name' => $data->company_name,
'access_token' => $access_token
]);
$response['user_id'] = $user->_id;
$response['access_token'] = $user->access_token;
return $response;
}
}
这就是我在UserController中使用RUser类的方式
And this is how i am using my RUser class in my UserController
<?php
namespace App\Http\Controllers;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use App\Models\User;
use Validator;
use FUser;
class UserController extends Controller
{
/**
* Signup User
* @param Request $request
* @return array $response
*/
public function store(Request $request)
{
$this->request = $request;
$payload = json_decode($this->request->getContent());
if($this->validateJson('user.create', $payload)) {
$validator = Validator::make((array) $payload, User::$rules);
if ($validator->fails()) {
$messages = $validator->errors();
return $this->sendResponse(false, $messages);
}
$response = FUser::signup($payload);
return $this->sendResponse(true, $response);
}
}
}
现在问题出在我的RUser类中,我希望Laravel自动注入TokenGenerator依赖关系,因为它通常会在Controller类中注入依赖关系.但是我不明白为什么它不这样做.
Now the problem is in my RUser class i am expecting Laravel to inject the TokenGenerator Dependency automatically as it normally inject dependencies in Controller classes. But i do not understand why it is not doing so.
这就是我得到的
RUser.php第15行中的ErrorException:参数1传递给 App \ Http \ Responses \ RUser :: __ construct()必须是 App \ Utils \ TokenGenerator,未给出,调用 /var/www/html/MyApp/app/Providers/RUserServiceProvider.php,第29行 并定义
ErrorException in RUser.php line 15: Argument 1 passed to App\Http\Responses\RUser::__construct() must be an instance of App\Utils\TokenGenerator, none given, called in /var/www/html/MyApp/app/Providers/RUserServiceProvider.php on line 29 and defined
推荐答案
如果希望容器注入依赖项,则需要使用容器的make()
方法,而不要使用本机new
运算符:
If you want the container to inject dependencies, you need to use the container's make()
method instead of the native new
operator:
App::bind('user', function($app) {
return $app->make('App\Http\Responses\RUser');
});
可以更简洁地写为:
App::bind('user', 'App\Http\Responses\RUser');
这篇关于Laravel DI在立面基础类中使用时不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!