我创建了一个自定义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\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
,似乎没有调用执行函数。我该如何解决这个问题?
答案 0 :(得分:1)
您需要将属性obj.Contains()
添加到您的班级:
protected $commandBus;