【问题标题】:Laravel Dependency Injection issue with Controller?控制器的 Laravel 依赖注入问题?
【发布时间】:2019-06-13 22:10:27
【问题描述】:

我设置了依赖注入,因为我觉得它应该进入我的病人控制器,但由于某种原因,它永远不会在最后一行执行对 index 函数的依赖,它甚至会在它之前返回 $request 数据,但对于一些reason 不会执行我尝试返回的 Patient Repository 中的数据。

我试图这样做: return (new Patient)->getByAccNumAndDateOrZip($this->client_code, $this->account_number, $this->dob, $this->zip);

另外,请记住,是的,所有 $requests 都具有有效值,并且不会返回空值或 null 值。

仍然没有得到任何回报....

namespace App\Http\Controllers\api;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;

//Repositories
use App\Repositories\Patient;

class PatientController extends Controller {
  private $patient;

  public function __construct(Patient $patient) {
    $this->middleware('auth.client');
    $this->patient = $patient;
  }

  public function index(Request $request) {
    //I can do return $request->client_code
    //But I can't use this dependency... It's weird...
    return $this->patient->getByAccNumAndDateOrZip($request->client_code, $request->account_number, $request->dob, $request->zip);
  }
}

我期待调用我的依赖项,该依赖项通过帐号拉动我的所有患者。依赖项只是一个标准类,其命名空间为 App\Repositories,它没有设置构造函数,只有几个标准公共函数,它们将在函数中获取特定变量。

【问题讨论】:

  • 您是否在服务提供商中注册了患者存储库?
  • @Amade 如果我理解他的评论,他说他的 Repository 类只是一个没有其他依赖项的标准类,所以他不需要注册它。
  • 如果我理解正确,如果您的应用程序没有执行getByAccNumAndDateOrZip 方法内的任何代码?如果是这样,错误日志中是否有任何内容?
  • 不,它只是返回所有空白。我也确实试图清除我正在调用的函数,没有数据进入它,并试图只返回“Test”;仍然一无所获。
  • 再次查看日志,说Patient类名已经被使用,改成PatientRepository并修复了!

标签: laravel dependency-injection


【解决方案1】:

查看 laravel 中的日志文件后,我可以看到系统声明该类名已被使用,因此我必须选择其他名称。

这是 Matt Wohler 建议检查日志的,男孩对我有帮助!

再次感谢大家的帮助!

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-05-17
    • 2016-02-01
    • 1970-01-01
    • 2021-12-26
    • 2018-10-21
    • 2015-12-10
    • 1970-01-01
    相关资源
    最近更新 更多