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.
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.
If you need to pass parameters to the constructor, please useApp::make('className')
.
The code implementation uses reflection to check whether the constructor needs to pass parameters:App::makeWith('className', [param1, param2, ...])
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::make
或App::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?