Php 未找到Laravel自定义命令总线
我创建了一个自定义APIController来扩展通用控制器。 自定义API控制器如下所示:Php 未找到Laravel自定义命令总线,php,laravel,Php,Laravel,我创建了一个自定义APIController来扩展通用控制器。 自定义API控制器如下所示: namespace App\Http\Controllers\API\v1; use App\Commanding\CommandBus; use App\Commanding\ValidationCommandBus; use League\Fractal\Manager; use League\Fractal\Resource\Collection; use League\Fractal\Res
namespace App\Http\Controllers\API\v1;
use App\Commanding\CommandBus;
use App\Commanding\ValidationCommandBus;
use League\Fractal\Manager;
use League\Fractal\Resource\Collection;
use League\Fractal\Resource\Item;
use Illuminate\Pagination\Paginator;
use Illuminate\Http\Response as IlluminateResponse;
use App\Http\Controllers\Controller as BaseController;
class APIController extends BaseController {
protected $statusCode = 200;
// Fractal API Manager
protected $fractal;
public function __construct(ValidationCommandBus $commandBus, Manager $fractal) {
// Init fractal manager
$this->fractal = $fractal;
$this->commandBus = $commandBus;
}
// Get the current status code
public function getStatusCode() {
return $this->statusCode;
}
// Set the current status code
public function setStatusCode($statusCode) {
$this->statusCode = $statusCode;
return $this;
}
// Respond with data and set status code
public function respond($data, $headers = []) {
return response()->json($data, $this->getStatusCode(), $headers, JSON_NUMERIC_CHECK);
}
protected function respondWithItem($item, $callback) {
$resource = new Item($item, $callback);
$rootScope = $this->fractal->createData($resource);
return $rootScope->toArray();
}
protected function respondWithCollection($collection, $callback) {
$resource = new Collection($collection, $callback);
$rootScope = $this->fractal->createData($resource);
return $rootScope->toArray();
}
protected function respondWithCollectionAndResourceKey($collection, $resourceKey, $callback) {
$resource = new Collection($collection, $callback, $resourceKey);
$rootScope = $this->fractal->createData($resource);
return $rootScope->toArray();
}
protected function respondWithPagination($paginationData, $data) {
$response = array_merge($data, $pagination = [
'pagination' => [
'total' => $paginationData->total(),
'perPage' => $paginationData->perPage(),
'lastPage' => $paginationData->lastPage(),
'nextPageUrl' => $paginationData->nextPageUrl(),
'prevPageUrl' => $paginationData->previousPageUrl(),
'from' => $paginationData->firstItem(),
'to' => $paginationData->lastItem(),
'totalPages' => ceil($paginationData->total() / $paginationData->perPage()),
'currentPage' => $paginationData->currentPage(),
'hasMorePages' => $paginationData->hasMorePages()
]
]);
return $this->respond($response);
}
// Respond with error
public function respondWithError($message) {
return $this->respond([
'error' => [
'message' => $message,
'status_code' => $this->getStatusCode()
]
]);
}
// Delete
public function respondWithSuccessfulDeletion($message) {
return $this->setStatusCode(200)->respond([
'message' => $message
]);
}
// Response Validation Failed
public function respondValidationFailed($message) {
return $this->setStatusCode(422)->respondWithError($message);
}
// Response Internal Error
public function respondWithInternalError($message) {
return $this->setStatusCode(500)->respond([
'message' => $message,
'status_code' => $this->getStatusCode()
]);
}
}
此控制器有一个名为commandBus
的属性,我希望通过使用此APIController扩展另一个控制器来访问该属性
class APIMFUserController extends APIController {...}
在PHPStorm中,当我调用
$this->commandBus->execute
时,我得到一个错误,即未找到字段commandBus
,并且似乎未调用execute函数。如何修复此问题?您需要添加属性protected$commandBus代码>到您的班级:
class APIController extends BaseController {
protected $statusCode = 200;
// Fractal API Manager
protected $fractal;
protected $commandBus;
public function __construct(ValidationCommandBus $commandBus, Manager $fractal) {
// Init fractal manager
$this->fractal = $fractal;
$this->commandBus = $commandBus;
}
是APIMFUserController
包含commandBus
var??