Can I use method dependency injection outside the controller?

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 gives me the sgwiggly line and says that the message "Required paramater $ param2 is missing".

Does such a thing only work in the controller, or can I make it work elsewhere? Or will it work if I run it and PHPstorm just thinks it is not?

http://laravel.com/docs/5.0/controllers#dependency-injection-and-controllers

0
source share
1 answer

, , a Container. , \App::make() , \App::call() .

Illuminate/Container/Container.php, , , - . , . , , . .

:

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

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

:

$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.
0

Source: https://habr.com/ru/post/1629029/


All Articles