小编Sec*_*Fry的帖子

Laravel DI:调用控制器方法而不传递注入变量,是否可能?

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)

php laravel

5
推荐指数
1
解决办法
128
查看次数

标签 统计

laravel ×1

php ×1