简体   繁体   English

依赖注入Laravel

[英]Dependency Injection Laravel

I have a Facade Class 我有门面课

use App\Http\Response 

class RUser
{
    public function signup(TokenGenerator $token_generator, $data)
    {
        $response = [];
        $access_token = $token_generator->generate(32);
        dd($access_token);
        $user = User::create($data);
        $response['user_id'] = $user->_id;
        $response[''];
    }
}

Now from my UserController I am trying to use the RUser::signup() function like below 现在,从我的UserController我试图使用RUser::signup()函数,如下所示

class UserController extends Controller
{
    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);
            }
            FUser::signup($payload);
            return $this->sendResponse(true, $response);
        }
    }
}

I think Laravel resolves these dependency automatically and i shound not be passing the instance of TokenGenerator explicitly. 我认为Laravel自动解决了这些依赖关系,而且我没有明确地传递TokenGenerator的实例。 But i am getting the following error. 但我收到以下错误。

Argument 1 passed to App\Http\Responses\RUser::signup() must be an instance of App\Utils\TokenGenerator, instance of stdClass given, called in 

Laravel injects instances in custom classes only in constructors by default. Laravel默认仅在构造函数中注入自定义类中的实例。 Controller actions will also receive instances injected. 控制器动作还将接收注入的实例。

So your example will need such changes to work properly: 因此,您的示例将需要进行以下更改才能正常工作:

class RUser
{
    protected $token_generator;

    public function __construct(TokenGenerator $token_generator)
    {
        $this->token_generator = $token_generator;
    }

    public function signup($data)
    {
        $response = [];
        $access_token = $this->token_generator->generate(32);
        dd($access_token);
        $user = User::create($data);
        $response['user_id'] = $user->_id;
        $response[''];
    }
}

and

class UserController extends Controller
{
    public function store(Request $request, RUser $ruser)
    {
        $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);
            }
            $ruser->signup($payload);
            return $this->sendResponse(true, $response);
        }
    }
}

To trigger dependency injection outside when you instantiate class on your own you need to call make() method of Illuminate\\Foundation\\Application instance, like this: 要在自己实例化类时在外部触发依赖项注入,需要调用Illuminate \\ Foundation \\ Application实例的make()方法,如下所示:

$fooBar = $this->app->make('FooBar');

See Laravel docs 参见Laravel文档

I think you are confused with the use of Facades and Dependency Injection. 我认为您对外观和依赖注入的使用感到困惑。 The thing you should follow here is dependence injection upto my knowledge you can use the class directly in here and it will work without any problem TO have a clear idea of when to use facades and when to use dependency injection check this link 据我所知,您应该遵循的是依存关系注入,据我所知,您可以在此处直接使用该类,并且可以毫无问题地工作, 以清楚了解何时使用外墙以及何时使用依存关系注入。检查此链接

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM