【发布时间】:2019-11-14 17:15:45
【问题描述】:
构建简单的 Laravel 后端应用。试图保持控制器清洁并将所有业务逻辑放入服务中,该服务将使用模型来查询数据。
我已经尝试了几个不同的选项(示例)在此处和文档中找到。 同时运行 composer dump-autoload 和 composer update。 仍然收到服务类不存在的错误。
CarController.php
<?php
namespace App\Http\Controllers;
use App\Services\CarService;
class CarController extends Controller
{
protected $carService;
public function __construct(CarService $carService)
{
$this->carService = $carService;
}
public function getCar()
{
return $carService::getCar();
}
}
?>
汽车服务.php
<?
namespace App\Services;
class CarService
{
public function getCar()
{
return 'Hello';
}
}
?>
在控制器中调用getCar函数,报错:
<?
namespace App\Services;
class CarService
{
public function getCar()
{
return 'Hello';
}
}
?>
我在邮递员中收到以下错误
{ "message": "类 App\Services\CarService 不存在", “异常”:“反射异常”, "文件": "/home/MyDev/BackEnd/vendor/laravel/framework/src/Illuminate/Container/Container.php", “线”:826, “痕迹”: [ { "文件": "/home/MyDev/BackEnd/vendor/laravel/framework/src/Illuminate/Container/Container.php", “线”:826, “函数”:“getClass”, “类”:“反射参数”, “类型”:“->”
(我正在使用 Postman 调用调用控制器的 API。 不知道为什么原始响应打印出 CarService 的内容并同时说它不存在...)
谢谢
【问题讨论】:
-
更新:我正在使用 Visual Studio Code,似乎当我将模型名称添加到 __construct 中时,它显示帮助悬停文本,这是 Car extends Model 的一个类。当我将它改回我的服务类时,它不能解决名称...不确定这是 VS Code 问题还是我缺少一些配置
标签: laravel service controller