我可以在控制器外部使用方法依赖注入吗? [英] can I use method dependency injection outside of a controller?

查看:67
本文介绍了我可以在控制器外部使用方法依赖注入吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我在某处具有以下功能:

if I have a function somewhere like:

public function test(Request $request, $param1, $param2)    

然后使用以下命令将其命名为其他地方

then call it somewhere else with:

$thing->test('abc','def')

PHPstorm给了我一个模糊的提示,并显示缺少必需的参数$ param2"消息.

PHPstorm gives me a sgwiggly line and says "Required paramater $param2 is missing" message.

这种事情仅在控制器中起作用吗,或者我可以使其在其他地方起作用吗?还是如果我运行它而PHPstorm认为它不起作用,它将起作用吗?

Does this sort of thing only work in a controller or can I make it work elsewhere? Or will it work if I ran it and PHPstorm just thinks it doesn't?

http://laravel.com/docs/5.0/controllers#依赖注入和控制器

推荐答案

是的,您可以在任何地方使用方法注入,但是必须通过Container调用该方法.就像使用\App::make()通过容器解析类实例一样,可以使用\App::call()通过容器调用方法.

Yes, you can use method injection anywhere, but you have to call the method through the a Container. Just like you use \App::make() to resolve a class instance through the container, you can use \App::call() to call a method through the container.

您可以检查Illuminate/Container/Container.php中的函数以获取所有详细信息,但通常,第一个参数是要调用的方法,第二个参数是要传递的参数数组.如果使用关联数组,则参数将按名称进行匹配,顺序无关紧要.如果使用索引数组,则可注入参数必须在方法定义中位于第一个位置,并将使用参数数组来填充其余参数.下面的示例.

You can inspect the function in Illuminate/Container/Container.php to get all the details, but in general the first parameter is the method to call and the second parameter is an array of parameters to pass. If an associative array is used, the parameters will be matched up by name and the order won't matter. If an indexed array is used, the injectable parameters must be first in the method definition, and the parameter array will be used to fill in the rest. Examples below.

提供以下课程:

class Thing {
    public function testFirst(Request $request, $param1, $param2) {
        return func_get_args();
    }

    public function testLast($param1, $param2, Request $request) {
        return func_get_args();
    }
}

您可以通过以下方式使用方法注入:

You can use method injection in the following ways:

$thing = new Thing();
// or $thing = App::make('Thing'); if you want.

// ex. testFirst with indexed array:
// $request will be resolved through container;
// $param1 = 'value1' and $param2 = 'value2'
$argsFirst = App::call([$thing, 'testFirst'], ['value1', 'value2']);

// ex. testFirst with associative array:
// $request will be resolved through container;
// $param1 = 'value1' and $param2 = 'value2'
$argsFirst = App::call([$thing, 'testFirst'], ['param1' => 'value1', 'param2' => 'value2']);

// ex. testLast with associative array:
// $param1 = 'value1' and $param2 = 'value2'
// $request will be resolved through container;
$argsLast = App::call([$thing, 'testLast'], ['param1' => 'value1', 'param2' => 'value2']);

// ex. testLast with indexed array:
// this will throw an error as it expects the injectable parameters to be first.

这篇关于我可以在控制器外部使用方法依赖注入吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆