If the parameters in the base controller's constructor are extended, would not the constructor automatically have the extended parameters without declaring?
Also, I know that you could declare constants or variables in your config and it will be global.
I am not sure if this helped any but there you go.
<?php
use MyNs\Commander\CommandBus;
use MyNs\Some\SomeRepository;
class SomeController extends BaseController {
protected $someRepo;
public function __construct(CommandBus $bus, SomeRepository $someRepo)
{
parent::__construct($bus);
$this->someRepo = $someRepo;
}
public function store()
{
$command = new Command(...);
$some = $this->bus->execute($command);
}
}
@damienadermann without redeclaring it. You've just repeated what is in the BaseController
.
I actually don't think there is a way to do it because of the way inheritance works and constructor precedence.
The laravel.io forum code doesn't have any solution to it either, so I guess I've already taken the optimization as far as it can go:
https://github.com/LaravelIO/laravel.io/blob/nextVersion/app/controllers/BaseController.php#L12
and
This has indeed nothing to do with Laravel, but just with php itself. You have to give the CommandBus object to the parent constructor, within the constructor itself. The laravel IoC is able to inject constructor properties, yes, but not like this unfortionately.
Would a solution perhaps be to initialize the command bus through a setCommandBus method? Or would there be specific cases where you would need the command bus available in the constructor?
Another solution would be to initialize the CommandBus inside the BaseController's constructor through App:make, instead of through Dependency Injection.
You can do this:
<?php
use MyNs\Commander\CommandBus;
class BaseController extends \Controller {
public $bus;
public function __construct()
{
$this->bus = App::make('MyNs\Commander\CommandBus');
}
...
Then in your controller
<?php
use MyNs\Some\SomeRepository;
class SomeController extends BaseController {
protected $someRepo;
public function __construct(SomeRepository $someRepo)
{
parent::__construct();
}
public function store()
{
$command = new Command(...);
$some = $this->bus->execute($command);
}
Hope you still need it :) works
Sign in to participate in this thread!
The Laravel portal for problem solving, knowledge sharing and community building.
The community