class SomeController extends Controller {
public function doALot(Request $request) {
$this -> doOne($someOtherVariable);
// Type error: Argument 1 passed to App\Http\Controllers\SomeController::doOne() must be an instance of Illuminate\Http\Request
$this -> doOne($request, $someOtherVariable);
// Bad practice?
...
}
public function doOne(Request $request, $someOtherVariable) {}
...
}
Run Code Online (Sandbox Code Playgroud)
那么如何doOne()在doALot()不传递注入资源的情况下进行调用,但是已经Request进入doOne()?传遍$request整个地方感觉不好.
解决方案tl; dr不可能,但还有其他方法 - 阅读Alexey Mezenin的简短回答
长版(可能不是最好但足够).
$ php artisan make:provider SomeServiceProvider
Run Code Online (Sandbox Code Playgroud)
然后在创建的提供程序编辑中register()调用某些内容:
public
function register() {
$this -> app -> bind('App\Services\SomeService', function ($app) …Run Code Online (Sandbox Code Playgroud)