sesc360 sesc360 - 1 month ago 13
PHP Question

Laravel Custom CommandBus not found

I created a custom APIController to extend generic controller.
The custom API Controller is this:

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()
]);
}
}


This controller has a property called
commandBus
which I would like to access by extending another controller with this APIController.

class APIMFUserController extends APIController {...}


In PHPStorm I get the error, when I call
$this->commandBus->execute
that the field
commandBus
has not been found and it seems the execute function does not get called. How do I fix this?

Answer

You need to add the property protected $commandBus; to your class:

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;
    }
Comments