php - How does laravel get a custom class through the container and pass parameters?
滿天的星座
滿天的星座 2017-05-16 12:59:28
0
3
730

Now the company's business needs to add a services layer based on the C layer to replace the controller for part of the business processing.
So I created a new Services folder in the app directory, and then called in the c layer controller:
$services = App::make('The complete class name of the service to be called');
Although the class specified in the services folder can be obtained, if the constructor of this class requires parameters, it cannot be passed at all.
I tried writing like this:
controller layer:

public function test(){
    $services = \App::make('App\Services\Servicetest',[1,2]);
    $services->test();
}

Write this in Servicetest.php in the app/services directory:

public function __construct($a, $b){
    echo $a;
    echo $b;
    echo 111;
    exit;
}
public function test(){
    echo '成功';
}

It’s very strange that when the instantiated object $services is obtained through the make method, its __construct() constructor is not triggered, and $a, $b and 111 are not output, but $services->test() does It was successfully executed and 'successful' was output.
If so, could you please tell me if I want to introduce and execute the classes in the services folder in the controller to share the load? How do I introduce the implementation of part of the business logic? I think the App::make() method is very good. It does not require manual require and directly obtains the instantiated object. But why is the instantiated object successfully obtained but not Executing the constructor outputs 111? How can I pass in parameters to the constructor of the services layer through App::make() on the c layer?
Looking forward to your help, thank you all.

滿天的星座
滿天的星座

reply all(3)
淡淡烟草味

I don’t know how you succeeded. At least I followed your approach and tested it locally, but it was unsuccessful (my Laravel is version 5.4).

I also looked at the relevant code. First of all, this method does not support passing parameters to the constructor. App::make('className')

If you need to pass parameters to the constructor, please use

. App::makeWith('className', [param1, param2, ...])

The code implementation uses reflection to check whether the constructor needs to pass parameters:

1. If no parameters are required, instantiate directly
2. If parameters need to be passed and the parameters are instantiable classes, try to instantiate This class (loop into the logic of
), and pass the instantiated class as a parameter to App::makeApp::makeWith()3. If parameters need to be passed, and the parameters are non-instantiable parameters (such as variables without type hints, the type hints are integers) , string, floating point, etc.), then further check whether there are default parameters. If there are default parameters, pass the default parameters to the constructor. If there are no default parameters, an exception will be thrown, as shown below:

黄舟

Forget about the static class directly. I later found out that it needs to be instantiated every time. Although automatic instantiation is injected into the controller, calling it in other places has to be done manually, so it is all static classed by me. . . . .

刘奇

Can’t dependency injection solve it?

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template